Accessing images in a jar file

kotuboy

Grünschnabel
Hallo,
At first I must say, Everything works in Eclipse.

I generate a jar file with Ant. When I start the program it doesn't find the images.

I do it like:
In eclipse there are two folders. One is src (for java files). The other is images(for images).
I put the classes and images in jar file with the following structure
-classes
-images
-META-INF

The code to read images is like:
ImageIcon img = new ImageIcon("http://www.tutorials.de/images/+ "image1.PNG")
JButton button = new JButton(img);

Hilfee******!
 
Hi

Try this:

Java:
import javax.imageio.*;
//...
InputStream is;
is=getClass().getClassLoader().getResourceAsStream("images/image1.PNG");
img=new ImageIcon(ImageIO.read(is));
is.close();

Don´t forget exception handling.
 
Zurück