Finding resources in JAR file

A

Abs

Hi!

I packaged my app in a signed JAR file and I can run it with no problems
from command line using:

java -jar myapp.jar

BUT, when I try running it with WebStart it fails to find its resources
(images, icons, etc.) which can be found inside the same JAR file. I
load them using the following line of code:

ImageIcon icon=new
ImageIcon(ResourceAnchor.class.getResource("icons"+System.getProperty("file.separator")+"icon.png")));


I tried the getClassLoader option, too:

ClassLoader loader=ResourceAnchor.class.getClassLoader();
ImageIcon icon=new
ImageIcon(loader.getResource("icons"+System.getProperty("file.separator")+"icon.png")));

But I get a NullPointerException. If I print this in the console:

System.out.println(loader.getResource("icons"+System.getProperty("file.separator")+"icon.png"));

I get "null" instead of the full image path as a result.



What am I doing wrong ?


Thanks in advance
 
A

Alex Kizub

BUT, when I try running it with WebStart it fails to find its resources
(images, icons, etc.) which can be found inside the same JAR file.

My two cents:

1) Don't use relative path. If you say "icon/abc.gif" it means in the same directory where class located
and "icon" down.
Which means that if this class is com.image.ppp.Test it should be in jar here com/image/ppp/icon/abc.gif.

Use absolute path but don't use root. So "/icon/abc.gif" is good, but "/abc.gif" is not. Don't know why...

2) Also be careful which class you use to take resources of. I suggest to use class which is in the same
jar file.

Alex Kizub.
 

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

No members online now.

Forum statistics

Threads
473,982
Messages
2,570,190
Members
46,736
Latest member
zacharyharris

Latest Threads

Top