views:

154

answers:

2

I can run my Java Swing application from Eclipse without problems. But when I run it from a .jar-file, the images fails to load.

I load my images with:

setIconImage(Toolkit.getDefaultToolkit().
getImage(getClass().getResource("../images/logo.png")));

How can I load the images so they work even when I'm running from a .jar-file?

The images is in the same Jar-file, in the packet com.example.images and the class where they are used is in com.example.gui.dialogs

+1  A: 

The images should be packed as well in a jar file. Actually, I'm not 100% sure there is no other solution, but at least I made it work this way, when experimenting the same issue.

The jar was then added to the classpath, and I'm accessing image resources this way:

getResource("images/logo.png");

Gnoupi
As what I know, they are in the jar-file already.
Jonas
@Jonas - Make sure their folder is correctly added to the classpath.
Gnoupi
+4  A: 

Use the absolut path inside your jar. If you don't know it, try opening the JAR with a zip programm, e.g. 7zip. Then use the absolute path:

getClass().getResource("/com/examples/images/logo.png")

This obiously only works when your image is in your jar. If its not, but in your classpath, this should be fine too.

ZeissS