resources from JAR files

Discussion in 'Java' started by bob smith, Jan 17, 2013.

  1. bob smith

    bob smith Guest

    I have some code that reads an image from its JAR file like so:

    img = ImageIO.read(frame.getClass().getResource("whatever.jpg"));

    I basically just picked the "frame" object at random to call the getClass() method on it.

    Is there a way to do this without picking an arbitrary object? It seems so wrong.
     
    bob smith, Jan 17, 2013
    #1
    1. Advertising

  2. bob smith

    markspace Guest

    On 1/17/2013 8:30 AM, bob smith wrote:
    > I have some code that reads an image from its JAR file like so:
    >
    > img = ImageIO.read(frame.getClass().getResource("whatever.jpg"));
    >
    > I basically just picked the "frame" object at random to call the getClass() method on it.
    >
    > Is there a way to do this without picking an arbitrary object? It seems so wrong.
    >



    Yes, normally I use the object the line of code is contained it.

    class MyBusinessObject {

    void someMethod() {
    Image img ImageIO.read( getClass().getResource( "whatever.jpg" ));
    // etc...
    }
    }

    You can also use a constant. MyBusinessObject.class.getResource(...)
    instead of getClass().

    You can also use
    Thread.currentThread().getContextClassLoader().getResource(...), but
    that's usually only useful in situations where thread contexts are used,
    i.e. web apps.
     
    markspace, Jan 17, 2013
    #2
    1. Advertising

  3. bob smith

    Roedy Green Guest

    On Thu, 17 Jan 2013 08:30:23 -0800 (PST), bob smith
    <> wrote, quoted or indirectly quoted someone
    who said :

    >I have some code that reads an image from its JAR file like so:
    >
    >img = ImageIO.read(frame.getClass().getResource("whatever.jpg"));
    >
    >I basically just picked the "frame" object at random to call the getClass() method on it.
    >
    >Is there a way to do this without picking an arbitrary object? It seems so wrong.


    It is really just looking for a package name to find the resource. You
    can name the main class explicitly MyProg.class or this.getClass()
    There is no Package class, so they use Class. You want something in
    YOUR package.

    see http://mindprod.com/jgloss/resource.html
    --
    Roedy Green Canadian Mind Products http://mindprod.com
    The first 90% of the code accounts for the first 90% of the development time.
    The remaining 10% of the code accounts for the other 90% of the development
    time.
    ~ Tom Cargill Ninety-ninety Law
     
    Roedy Green, Jan 17, 2013
    #3
  4. bob smith

    Arne Vajhøj Guest

    On 1/17/2013 11:30 AM, bob smith wrote:
    > I have some code that reads an image from its JAR file like so:
    >
    > img = ImageIO.read(frame.getClass().getResource("whatever.jpg"));
    >
    > I basically just picked the "frame" object at random to call the getClass() method on it.
    >
    > Is there a way to do this without picking an arbitrary object? It seems so wrong.


    You need to pick either an object or a class.

    I think it is relative common to pick either this or the class.

    Arne
     
    Arne Vajhøj, Jan 17, 2013
    #4
  5. bob smith

    Lew Guest

    Lew, Jan 17, 2013
    #5
  6. bob smith

    Roedy Green Guest

    On Thu, 17 Jan 2013 15:16:47 -0800 (PST), Lew <>
    wrote, quoted or indirectly quoted someone who said :

    >Roedy Green wrote:
    >> There is no Package class, ...

    >
    >http://docs.oracle.com/javase/7/docs/api/java/lang/Package.html


    That came in 1.5, a fair bit after getResource.
    The one advantage of using class is it unambiguously specifies a jar,
    even a jar whose name is unknown.
    --
    Roedy Green Canadian Mind Products http://mindprod.com
    The first 90% of the code accounts for the first 90% of the development time.
    The remaining 10% of the code accounts for the other 90% of the development
    time.
    ~ Tom Cargill Ninety-ninety Law
     
    Roedy Green, Jan 18, 2013
    #6
  7. bob smith

    Arne Vajhøj Guest

    On 1/17/2013 7:36 PM, Roedy Green wrote:
    > On Thu, 17 Jan 2013 15:16:47 -0800 (PST), Lew <>
    > wrote, quoted or indirectly quoted someone who said :
    >
    >> Roedy Green wrote:
    >>> There is no Package class, ...

    >>
    >> http://docs.oracle.com/javase/7/docs/api/java/lang/Package.html

    >
    > That came in 1.5, a fair bit after getResource.


    But 1.5 came out in 2004.

    A fair bit of time ago.

    Arne
     
    Arne Vajhøj, Jan 18, 2013
    #7
  8. bob smith

    BGB Guest

    On 1/17/2013 5:13 PM, Arne Vajhøj wrote:
    > On 1/17/2013 11:30 AM, bob smith wrote:
    >> I have some code that reads an image from its JAR file like so:
    >>
    >> img = ImageIO.read(frame.getClass().getResource("whatever.jpg"));
    >>
    >> I basically just picked the "frame" object at random to call the
    >> getClass() method on it.
    >>
    >> Is there a way to do this without picking an arbitrary object? It
    >> seems so wrong.

    >
    > You need to pick either an object or a class.
    >
    > I think it is relative common to pick either this or the class.
    >


    FWIW, AFIAK:
    it can also be noted that it does matter which class you pick, like
    generally you want the class and resource file to be in the same package
    and JAR and similar (the class basically telling the JVM where to look,
    otherwise the resource may not be found).

    so, while a person can pick an arbitrary class, it may not necessarily
    find the resource.

    so, generally, picking 'this' or similar makes sense, since normally a
    person will package the resources along with their own code.


    or such...
     
    BGB, Jan 25, 2013
    #8
  9. bob smith

    Arne Vajhøj Guest

    On 1/25/2013 4:08 PM, BGB wrote:
    > On 1/17/2013 5:13 PM, Arne Vajhøj wrote:
    >> On 1/17/2013 11:30 AM, bob smith wrote:
    >>> I have some code that reads an image from its JAR file like so:
    >>>
    >>> img = ImageIO.read(frame.getClass().getResource("whatever.jpg"));
    >>>
    >>> I basically just picked the "frame" object at random to call the
    >>> getClass() method on it.
    >>>
    >>> Is there a way to do this without picking an arbitrary object? It
    >>> seems so wrong.

    >>
    >> You need to pick either an object or a class.
    >>
    >> I think it is relative common to pick either this or the class.
    >>

    >
    > FWIW, AFIAK:
    > it can also be noted that it does matter which class you pick, like
    > generally you want the class and resource file to be in the same package
    > and JAR and similar (the class basically telling the JVM where to look,
    > otherwise the resource may not be found).
    >
    > so, while a person can pick an arbitrary class, it may not necessarily
    > find the resource.
    >
    > so, generally, picking 'this' or similar makes sense, since normally a
    > person will package the resources along with their own code.
    >
    >
    > or such...


    I would use "the class" of this, so ...

    And the class method can be used in static context.

    Arne
     
    Arne Vajhøj, Jan 26, 2013
    #9
  10. bob smith

    Lew Guest

    Arne Vajhøj wrote:
    > BGB wrote:
    >> FWIW, AFIAK:
    >> it can also be noted that it does matter which class you pick, like


    That's partly correct, as five minutes' reading of the 'getResource()' method docs will
    reveal. What actually matters is what classloader you pick; any number of classes (and
    frequently all the ones you have access to) are loaded by one classloader.

    As for the seeming arbitrariness of the idiom that started this thread, perhaps the
    programmer picked a class known to have the correct classloader, is all.

    >> generally you want the class and resource file to be in the same package


    That's not true.

    >> and JAR and similar (the class basically telling the JVM where to look,


    That's not true either, and that's not true.

    You can have the resource in any package that makes sense. Common conventions
    are 'resource', 'resources', 'res', or those relative to the "official" package root of your
    application, such as 'com.lewscanon.slicedbread.resources'.

    And as stated, it's "basically" the classloader telling the JVM where to look.

    >> otherwise the resource may not be found).


    The resource will be found if it's in the location specified by the argument to the call.

    That can be the default package, the same package, a different package, or whatever.

    Same JAR, different JAR, remote URL, whatever.

    > > so, while a person can pick an arbitrary class, it may not necessarily
    > > find the resource.


    You should never pick an arbitrary class, however you may pick an arbitrary
    class from the set of those that use the appropriate classloader, or even use
    the classloader directly. It's all good. If you read the Javadocs, you willnot guess
    but know that the call is correct.

    >> so, generally, picking 'this' or similar makes sense, since normally a


    Because generally you want the same classloader as the caller's.

    What is "or similar"?

    >> person will package the resources along with their own code.


    Normally by what metric?

    You put the resources where the architecture of the system mandates. Again,
    that can be remote - quite common for applets in their day - from a JAR,
    from anywhere accessible to a classloader. You are correct to the extent that
    the default classloader is often the one you want, so 'this' or 'Type.class' do just
    fine. But that's an arbitrary choice.

    >> or such...


    False analogy.

    > I would use "the class" of this, so ...
    > And the class method can be used in static context.


    Note: He means "method" in the English sense here, as in "means of getting to the classloader",
    not as in "the class literal is a Java method".

    Bear in mind that the 'Class' version of 'getResource()' is a convenience method - the
    'ClassLoader' version is the workhorse.

    Don't use these calls by rule of thumb as BGB suggests. Breaking into 'Class' methods and more so
    'ClassLoader' methods is of the world of reflection, and classpaths, and package-to-real-world
    connections, and stuff that that breaks type safety. This is part of the heart of what makes Java Java.
    This is stuff you need to actually know, not do by cargo-cult programming.

    --
    Lew
     
    Lew, Jan 26, 2013
    #10
    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:
    495
    Roedy Green
    May 24, 2004
  2. Arnold Peters
    Replies:
    0
    Views:
    611
    Arnold Peters
    Jan 5, 2005
  3. muttley
    Replies:
    0
    Views:
    2,767
    muttley
    Oct 20, 2005
  4. cyberco
    Replies:
    4
    Views:
    3,844
    Roedy Green
    Feb 14, 2006
  5. Arnold Peters
    Replies:
    0
    Views:
    689
    Arnold Peters
    Jan 5, 2005
Loading...

Share This Page