Can't find file via class.getResource()

Discussion in 'Java' started by Ryan, Oct 17, 2007.

  1. Ryan

    Ryan Guest

    I know, I know... if the file's location is on the classpath, it
    should work. Well, it doesn't. No matter what I try.

    I have a simple app with a toolbar which I'm putting icons on. I have
    a very simple directory structure....

    - bin - holds a run scripts
    - lib - contains all needed jar files
    - images - contains gifs, jpgs etc etc
    - audio - contains audio files

    My code create a JButton with an Action and adds it to a JToolbar.
    That Action....

    putValue(SMALL_ICON, createIcon(smallIcon));

    where the icon is created by.....

    URL imageURL = getClass().getResource("test.gif");
    if (imageURL == null)
    {
    System.out.println("Resource not found");
    return null;
    }
    return new ImageIcon(imageURL);


    What is the deal? I've substituted "test.gif" with "../images/
    test.gif" and every other possibility. I've even copied the gif files
    into the jar file (which clearly is on the classpath since the app is
    up and running) and it still can't find the resource.

    Any thoughts would be great.
    Ryan, Oct 17, 2007
    #1
    1. Advertising

  2. Ryan

    Mark Space Guest

    Ryan wrote:

    > - bin - holds a run scripts
    > - lib - contains all needed jar files
    > - images - contains gifs, jpgs etc etc
    > - audio - contains audio files


    Which one of these is on your class path? Where? Where are the .class
    files? Is this inside a .jar? Is this inside an IDE? Give us a bit
    more info here. e.g. copy-paste your classpath so we can see it,
    copy-paste a directory listing of your project, etc.


    > What is the deal? I've substituted "test.gif" with "../images/
    > test.gif" and every other possibility. I've even copied the gif files


    Just a hunch, try "/images/test.gif"

    > into the jar file (which clearly is on the classpath since the app is
    > up and running) and it still can't find the resource.


    Where in the jar file? root? /images? what?
    Mark Space, Oct 17, 2007
    #2
    1. Advertising

  3. Andrew Thompson, Oct 18, 2007
    #3
  4. Ryan a écrit :
    > I know, I know... if the file's location is on the classpath, it
    > should work. Well, it doesn't. No matter what I try.
    >
    > I have a simple app with a toolbar which I'm putting icons on. I have
    > a very simple directory structure....
    >
    > - bin - holds a run scripts
    > - lib - contains all needed jar files
    > - images - contains gifs, jpgs etc etc
    > - audio - contains audio files
    >
    > My code create a JButton with an Action and adds it to a JToolbar.
    > That Action....
    >
    > putValue(SMALL_ICON, createIcon(smallIcon));
    >
    > where the icon is created by.....
    >
    > URL imageURL = getClass().getResource("test.gif");


    This code is searching for a file in the classpath, in the same package
    as the class on which the method above is called.
    Put the image file next to your .class file.
    Also, if the class is named MyClass, rather use
    MyClass.class.getResource() rather than getClass().getResource, else you
    will have to put the image file next to every subclass of MyClass.

    JB.
    Jean-Baptiste Nizet, Oct 18, 2007
    #4
    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. Manish Hatwalne

    getResource()

    Manish Hatwalne, Feb 9, 2004, in forum: Java
    Replies:
    7
    Views:
    5,975
    Andrew Thompson
    Feb 10, 2004
  2. David Favro

    ClassLoader.getResource() failure

    David Favro, Jun 19, 2004, in forum: Java
    Replies:
    1
    Views:
    24,999
    Jim Sculley
    Jun 19, 2004
  3. Replies:
    0
    Views:
    1,035
  4. Todd
    Replies:
    5
    Views:
    25,909
    Raymond DeCampo
    Jul 23, 2005
  5. kempshall

    Class.getResource path resolution

    kempshall, Jan 3, 2006, in forum: Java
    Replies:
    2
    Views:
    25,835
    Chris Smith
    Jan 4, 2006
Loading...

Share This Page