48

When running a Java app from eclipse my ImageIcon shows up just fine.

But after creating a jar the path to the image obviously gets screwed up.

Is there a way to extract an image from the jar at runtime so I can then open it up? Or, is there a better way to do this?

I'd like to distribute a single jar file if possible.

Barett
  • 5,826
  • 6
  • 51
  • 55
jjnguy
  • 136,852
  • 53
  • 295
  • 323

5 Answers5

37

To create an ImageIcon from an image file within the same jars your code is loaded:

new javax.swing.ImageIcon(getClass().getResource("myimage.jpeg"))

Class.getResource returns a URL of a resource (or null!). ImageIcon has a constructors that load from a URL.

To construct a URL for a resource in a jar not on your "classpath", see the documentation for java.net.JarURLConnection.

Tom Hawtin - tackline
  • 145,806
  • 30
  • 211
  • 305
  • This answer does not say where to put the image file in the jar, which is the tricky part. – arcy Nov 02 '20 at 02:27
24

You can try something like:

InputStream stream = this.getClass().getClassLoader().getResourceAsStream("/images/image.jpg");

In your JAR file, you might have a directory structure of:

MyJAR.jar
- com (class files in here)
- images
----image.jpg

Tim Frey
  • 9,901
  • 9
  • 44
  • 60
12

This is working for me to load and set the content pane background image:

jar (or build path) contains:

 - com
 - img
 ---- bg.png

java contains:

JFrame f = new JFrame("Testing load resource from jar");
try {
    BufferedImage bg = ImageIO.read(getClass().getResource("/img/bg.png"));
    f.setContentPane(new ImagePanel(bg));
} catch (IOException e) {
    e.printStackTrace();
}

Tested and working in both jar and unjarred (is that the technical term) execution.

BTW getClass().getClassLoader().getResourceAsStream("/img/bg.png") - which I tried first - returned me a null InputStream.

user229044
  • 232,980
  • 40
  • 330
  • 338
ekerner
  • 5,650
  • 1
  • 37
  • 31
  • getResourceAsStream returning NPE is it something to do with java version? I also got NPE with getResourceAsStream but getResource works perfectly. – Mrinal Bhattacharjee Sep 08 '15 at 07:24
4

In netbeans 8.1 what I've done is to include the folder of icons and other images called Resources inside the src folder in the project file. So whenever i build Jar file the folder is included there.The file tree should be like this:

  • src (Java files in source packges are here)
  • ** PACKAGE YOU NAMED IN PROJECT**

    • file.java
  • Resources

    • image.jpg

The code should be like:

jToggleButton1.setIcon(new javax.swing.ImageIcon(this.getClass().getResource("/resources/image.jpg")));
Abdelsalam Shahlol
  • 1,621
  • 1
  • 20
  • 31
0

Load image in from Jar file during run time is the same as loading image when executed from IDE e.g netbeans the difference is that when loading image from JAR file the path must be correct and its case sensitive (very important). This works for me

 image1 = new javax.swing.ImageIcon(getClass().getResource("/Pictures/firstgame/habitat1.jpg"));
        img = image1.getImage().getScaledInstance(lblhabitat1.getWidth(), lblhabitat1.getHeight(), Image.SCALE_SMOOTH);
        lblhabitat1.setIcon(new ImageIcon(img));

if p in "/Pictures/firstgame/habitat1.jpg" is in lower case it wont work. check spaces, cases and spelling

Tobore Igbe
  • 61
  • 1
  • 4
  • Another possible reason could be the extension of the image. I noticed that getClass().getResource("/Pictures/firstgame/habitat1.jpg") will display and load well from the IDE even if you save the image as "habitat1.JPG" but will not load from a jar file. You must save the extension with lower case as "habitat1.jpg" – Tobore Igbe Dec 29 '15 at 10:35