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

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top