Reputation: 103
I'm trying to launch a JAR file that has images that are used in the program. However it can't find them so the labels go blank. How do I go about fixing this?
Do I need all of the images in the same folder of the JAR folder and go into my code and edit the directory or would that not work?
Edit, Images obtained thusly:
hex19.setIcon(new ImageIcon("src/hexHouse.png")); //How the images are used.
Upvotes: 1
Views: 1935
Reputation: 285430
Your problem is that you are in fact trying to get the images as files, and files do not exist inside pf jars. Instead you must get the images as resources.
i.e.,
BufferedImage img = ImageIO.read(getClass().getResourceAsStream("imageStreamLocation.png"));
ImageIcon icon = new ImageIcon(img);
hex19.setIcon(icon);
The key will be using the correct image stream location. This should be a relative path to the "file" but relative to the location of the jar's class files.
Upvotes: 4