views:

720

answers:

2

I'm exporting a simple java project that includes two directories; src and Icons. Icons is a directory that contains three .png files.

I'm exporting to an executable .jar file using File -> Export. The export works properly and the .jar file contains the Icon directory. But I can't get the correct path for the .png files when the project is deployed. During the development I'm using the following path:

Icons/picture.png

and it works as long as I run from within the Eclipse IDE. How do I get the correct path for the icons?

+6  A: 

Your code is looking for the image outside of the .jar file. Try the URL constructor of ImageIcon instead.

Icon icon = new ImageIcon(getClass().getResource("Icons/picture.png"));

See Class.getResource().

Michael Myers
+4  A: 

mmyers is correct, but be aware that getClass().getResource() will load resources relative to the package where the class is defined. I suspect your icons are packaged at the root of the jar file and not relative to the class itself. To get resources from the root of the classpath, try: getClass().getClassLoader().getResourceAsStream("Icons/picture.png")

ThisIsTheDave