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.
Mark Space - 17 Oct 2007 23:09 GMT
> - 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?
Andrew Thompson - 18 Oct 2007 00:03 GMT
...
>Any thoughts would be great.
Since the success or failure of getResource() can be
determined by where it is called from the code, I suggest
you post an SSCCE* that demonstrates that. Try and
find the source file itself, for the code example.
* <http://www.physci.org/codes/sscce.html>

Signature
Andrew Thompson
http://www.athompson.info/andrew/
Jean-Baptiste Nizet - 18 Oct 2007 12:55 GMT
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.
[quoted text clipped - 15 lines]
>
> 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.