Reading resource files from a jar file

Discussion in 'Java' started by Martin Gregorie, Feb 12, 2010.

  1. I have a configuration file intentionally buried in a jar file I'm
    building (and a tool to amend it before you ask). This is all working
    correctly but could possibly have been done better.

    Yesterday I searched for documentation on doing this type of thing but
    couldn't find anything in the standard Java 6 documentation.
    Embarrassingly, I can't even find the description of how to set it up
    that I used in the first place. I'm probably missing something obvious,
    so pointers on where to find this information would be most welcome.

    The reason I need the documentation: I can write code that uses an
    InputStream to read the configuration file provided its in the same
    package as the class that reads it, but would prefer to put the file in
    the root of the jar file. Is it mandatory for the file to be in the same
    package as its reader or is there a way of accessing it when its placed
    elsewhere?


    --
    martin@ | Martin Gregorie
    gregorie. | Essex, UK
    org |
    Martin Gregorie, Feb 12, 2010
    #1
    1. Advertising

  2. Martin Gregorie

    Nigel Wade Guest

    On Fri, 12 Feb 2010 14:55:08 +0000, Martin Gregorie wrote:

    > I have a configuration file intentionally buried in a jar file I'm
    > building (and a tool to amend it before you ask). This is all working
    > correctly but could possibly have been done better.
    >
    > Yesterday I searched for documentation on doing this type of thing but
    > couldn't find anything in the standard Java 6 documentation.
    > Embarrassingly, I can't even find the description of how to set it up
    > that I used in the first place. I'm probably missing something obvious,
    > so pointers on where to find this information would be most welcome.
    >
    > The reason I need the documentation: I can write code that uses an
    > InputStream to read the configuration file provided its in the same
    > package as the class that reads it, but would prefer to put the file in
    > the root of the jar file. Is it mandatory for the file to be in the same
    > package as its reader or is there a way of accessing it when its placed
    > elsewhere?


    Specify a class within the relevant jar as the "reference" for getClass,
    then use an absolute path for the resource itself.

    --
    Nigel Wade
    Nigel Wade, Feb 12, 2010
    #2
    1. Advertising

  3. In article <hl411c$6f$>,
    Nigel Wade <> wrote:

    > On Fri, 12 Feb 2010 14:55:08 +0000, Martin Gregorie wrote:
    >
    > > I have a configuration file intentionally buried in a jar file I'm
    > > building (and a tool to amend it before you ask). This is all
    > > working correctly but could possibly have been done better.
    > >
    > > Yesterday I searched for documentation on doing this type of thing
    > > but couldn't find anything in the standard Java 6 documentation.
    > > Embarrassingly, I can't even find the description of how to set it
    > > up that I used in the first place. I'm probably missing something
    > > obvious, so pointers on where to find this information would be
    > > most welcome.
    > >
    > > The reason I need the documentation: I can write code that uses an
    > > InputStream to read the configuration file provided its in the same
    > > package as the class that reads it, but would prefer to put the
    > > file in the root of the jar file. Is it mandatory for the file to
    > > be in the same package as its reader or is there a way of accessing
    > > it when its placed elsewhere?

    >
    > Specify a class within the relevant jar as the "reference" for
    > getClass, then use an absolute path for the resource itself.


    Here's an example:

    <http://robotchase.svn.sourceforge.net/viewvc/robotchase/trunk/src/org/gc
    s/robot/RCImage.java?revision=64&view=markup>

    --
    John B. Matthews
    trashgod at gmail dot com
    <http://sites.google.com/site/drjohnbmatthews>
    John B. Matthews, Feb 12, 2010
    #3
  4. On Fri, 12 Feb 2010 12:24:48 -0500, John B. Matthews wrote:

    > In article <hl411c$6f$>,
    > Nigel Wade <> wrote:
    >
    >> On Fri, 12 Feb 2010 14:55:08 +0000, Martin Gregorie wrote:
    >>
    >> > I have a configuration file intentionally buried in a jar file I'm
    >> > building (and a tool to amend it before you ask). This is all working
    >> > correctly but could possibly have been done better.
    >> >
    >> > Yesterday I searched for documentation on doing this type of thing
    >> > but couldn't find anything in the standard Java 6 documentation.
    >> > Embarrassingly, I can't even find the description of how to set it up
    >> > that I used in the first place. I'm probably missing something
    >> > obvious, so pointers on where to find this information would be most
    >> > welcome.
    >> >
    >> > The reason I need the documentation: I can write code that uses an
    >> > InputStream to read the configuration file provided its in the same
    >> > package as the class that reads it, but would prefer to put the file
    >> > in the root of the jar file. Is it mandatory for the file to be in
    >> > the same package as its reader or is there a way of accessing it when
    >> > its placed elsewhere?

    >>
    >> Specify a class within the relevant jar as the "reference" for
    >> getClass, then use an absolute path for the resource itself.

    >
    > Here's an example:
    >
    > <http://robotchase.svn.sourceforge.net/viewvc/robotchase/trunk/src/org/

    gc
    > s/robot/RCImage.java?revision=64&view=markup>


    So the 'absolute path' takes the class's package as its root. Gotcha.


    --
    martin@ | Martin Gregorie
    gregorie. | Essex, UK
    org |
    Martin Gregorie, Feb 12, 2010
    #4
  5. Martin Gregorie

    Roedy Green Guest

    On Fri, 12 Feb 2010 14:55:08 +0000 (UTC), Martin Gregorie
    <> wrote, quoted or indirectly quoted
    someone who said :

    >I have a configuration file intentionally buried in a jar file I'm
    >building (and a tool to amend it before you ask). This is all working
    >correctly but could possibly have been done better.


    see http://mindprod.com/jgloss/resource.html
    and http://mindprod.com/jgloss/image.html
    for a number of examples.

    The trickiest part is deciding whether to put the resource in a
    package or in the root, then looking in the right place for it.
    --
    Roedy Green Canadian Mind Products
    http://mindprod.com

    Every compilable program in a sense works. The problem is with your unrealistic expections on what it will do.
    Roedy Green, Feb 12, 2010
    #5
  6. In article <hl44u0$p9k$>,
    Martin Gregorie <> wrote:

    > On Fri, 12 Feb 2010 12:24:48 -0500, John B. Matthews wrote:
    >
    > > In article <hl411c$6f$>,
    > > Nigel Wade <> wrote:
    > >
    > >> On Fri, 12 Feb 2010 14:55:08 +0000, Martin Gregorie wrote:
    > >>
    > >> > I have a configuration file intentionally buried in a jar file I'm
    > >> > building (and a tool to amend it before you ask). This is all working
    > >> > correctly but could possibly have been done better.
    > >> >
    > >> > Yesterday I searched for documentation on doing this type of thing
    > >> > but couldn't find anything in the standard Java 6 documentation.
    > >> > Embarrassingly, I can't even find the description of how to set it up
    > >> > that I used in the first place. I'm probably missing something
    > >> > obvious, so pointers on where to find this information would be most
    > >> > welcome.
    > >> >
    > >> > The reason I need the documentation: I can write code that uses an
    > >> > InputStream to read the configuration file provided its in the same
    > >> > package as the class that reads it, but would prefer to put the file
    > >> > in the root of the jar file. Is it mandatory for the file to be in
    > >> > the same package as its reader or is there a way of accessing it when
    > >> > its placed elsewhere?
    > >>
    > >> Specify a class within the relevant jar as the "reference" for
    > >> getClass, then use an absolute path for the resource itself.

    > >
    > > Here's an example:
    > >
    > > <http://robotchase.svn.sourceforge.net/viewvc/robotchase/trunk/src/org/

    > gcs/robot/RCImage.java?revision=64&view=markup>
    >
    > So the 'absolute path' takes the class's package as its root. Gotcha.


    Empirically, yes. It's my best interpretation of the getResource() API:

    <http://java.sun.com/javase/6/docs/api/java/lang/Class.html#getResource(java.lang.String)>

    --
    John B. Matthews
    trashgod at gmail dot com
    <http://sites.google.com/site/drjohnbmatthews>
    John B. Matthews, Feb 12, 2010
    #6
  7. Martin Gregorie

    Lew Guest

    Martin Gregorie <> wrote:
    >> So the 'absolute path' takes the class's package as its root. Gotcha.


    John B. Matthews wrote:
    > Empirically, yes. It's my best interpretation of the getResource() API:
    >
    > <http://java.sun.com/javase/6/docs/api/java/lang/Class.html#getResource(java.lang.String)>


    I think you have that backwards.

    AIUI, an absolute resource path (starts with '/') is relative to the class
    path, and a relative path (doesn't start with '/') is relative to the
    "modified_package_name".

    I am citing from the link provided. Plus, when I've run tests, I've observed
    behavior consistent with my interpretation.

    The wiggle room comes from
    "The rules for searching resources associated with a given class are
    implemented by the defining class loader of the class."

    So class loaders can change these rules.

    --
    Lew
    Lew, Feb 12, 2010
    #7
  8. Martin Gregorie

    Daniel Pitts Guest

    On 2/12/2010 6:55 AM, Martin Gregorie wrote:
    > I have a configuration file intentionally buried in a jar file I'm
    > building (and a tool to amend it before you ask). This is all working
    > correctly but could possibly have been done better.
    >
    > Yesterday I searched for documentation on doing this type of thing but
    > couldn't find anything in the standard Java 6 documentation.
    > Embarrassingly, I can't even find the description of how to set it up
    > that I used in the first place. I'm probably missing something obvious,
    > so pointers on where to find this information would be most welcome.
    >
    > The reason I need the documentation: I can write code that uses an
    > InputStream to read the configuration file provided its in the same
    > package as the class that reads it, but would prefer to put the file in
    > the root of the jar file. Is it mandatory for the file to be in the same
    > package as its reader or is there a way of accessing it when its placed
    > elsewhere?
    >
    >

    There is Class.getResource*, but you probably want ClassLoader.getResource*

    My understanding is that The Class.getResource looks in the same package
    as the class, where ClassLoader.getResource looks relative to the
    class-path roots which it knows about.

    --
    Daniel Pitts' Tech Blog: <http://virtualinfinity.net/wordpress/>
    Daniel Pitts, Feb 12, 2010
    #8
  9. Martin Gregorie

    Lew Guest

    On 2/12/2010 6:13 PM, Daniel Pitts wrote:
    > There is Class.getResource*, but you probably want ClassLoader.getResource*
    >
    > My understanding is that The Class.getResource looks in the same package
    > as the class, where ClassLoader.getResource looks relative to the
    > class-path roots which it knows about.


    See the link provided by John B. Matthews and the corresponding entry in
    the 'ClassLoader' Javadocs.

    The 'Class' version's behavior depends on the presence or absence of a
    leading '/' (slash) in the resource path. Without the leading slash,
    the resource is found relative the class's package, with the slash it's
    to the class path.

    --
    Lew
    Lew, Feb 12, 2010
    #9
  10. On Fri, 12 Feb 2010 18:45:32 -0500, Lew wrote:

    > On 2/12/2010 6:13 PM, Daniel Pitts wrote:
    >> There is Class.getResource*, but you probably want
    >> ClassLoader.getResource*
    >>
    >> My understanding is that The Class.getResource looks in the same
    >> package as the class, where ClassLoader.getResource looks relative to
    >> the class-path roots which it knows about.

    >
    > See the link provided by John B. Matthews and the corresponding entry in
    > the 'ClassLoader' Javadocs.
    >
    > The 'Class' version's behavior depends on the presence or absence of a
    > leading '/' (slash) in the resource path. Without the leading slash,
    > the resource is found relative the class's package, with the slash it's
    > to the class path.


    Thanks to all in this thread. I've made notes, read the docs for the
    Class and ClassLoader methods and now think I know enough to do what I
    wanted to do.


    --
    martin@ | Martin Gregorie
    gregorie. | Essex, UK
    org |
    Martin Gregorie, Feb 13, 2010
    #10
  11. In article <hl4m2m$jkj$>, Lew <>
    wrote:

    > Martin Gregorie <> wrote:
    > >> So the 'absolute path' takes the class's package as its root. Gotcha.

    >
    > John B. Matthews wrote:
    > > Empirically, yes. It's my best interpretation of the getResource() API:
    > >
    > > <http://java.sun.com/javase/6/docs/api/java/lang/Class.html#getResource(java

    ..lang.String)>
    >
    > I think you have that backwards.
    >
    > AIUI, an absolute resource path (starts with '/') is relative to the class
    > path, and a relative path (doesn't start with '/') is relative to the
    > "modified_package_name".
    >
    > I am citing from the link provided. Plus, when I've run tests, I've observed
    > behavior consistent with my interpretation.
    >
    > The wiggle room comes from
    > "The rules for searching resources associated with a given class are
    > implemented by the defining class loader of the class."
    >
    > So class loaders can change these rules.


    You've elucidated a subtle point that I'd overlooked previously: The
    leading '/' is a convenient notation in Class#getResource(); package
    names don't have a leading '.' to modify. The corresponding method in
    ClassLoader returns null when given a leading '/'.

    --
    John B. Matthews
    trashgod at gmail dot com
    <http://sites.google.com/site/drjohnbmatthews>
    John B. Matthews, Feb 13, 2010
    #11
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Eric McIntyre

    Jar files in Jar files

    Eric McIntyre, May 24, 2004, in forum: Java
    Replies:
    1
    Views:
    457
    Roedy Green
    May 24, 2004
  2. Arnold Peters
    Replies:
    0
    Views:
    543
    Arnold Peters
    Jan 5, 2005
  3. muttley
    Replies:
    0
    Views:
    2,677
    muttley
    Oct 20, 2005
  4. cyberco
    Replies:
    4
    Views:
    3,707
    Roedy Green
    Feb 14, 2006
  5. Arnold Peters
    Replies:
    0
    Views:
    631
    Arnold Peters
    Jan 5, 2005
Loading...

Share This Page