Can't find file via class.getResource()

R

Ryan

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.
 
M

Mark Space

Ryan said:
- 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?
 
A

Andrew Thompson

J

Jean-Baptiste Nizet

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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,982
Messages
2,570,190
Members
46,740
Latest member
AdolphBig6

Latest Threads

Top