Java Swing: displaying images from inside a jar

When starting a Java application from eclipse, my ImageIcon is displayed just fine.

But after creating the can, the path to the image is clearly screwed up.

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

I would like to distribute a single jar file if possible.

+42
java swing embedded-resource imageicon
Aug 27 '08 at 20:14
source share
5 answers

To create ImageIcon from an image file in the same banks, your code is loaded:

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

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

To create a resource URL in a bank not in your "classpath", see the documentation for java.net.JarURLConnection .

+31
Sep 05 '08 at 11:30
source share

You can try something like:

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

In your JAR file, you may have a directory structure:

myJar.jar
- com (class files here)
- Images
---- image.jpg

+21
Aug 27 '08 at 20:18
source share

This works to download and set the background image of the content panel:

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 works in both jar and unjarred (this is a technical term).

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

+9
Mar 12 '13 at 5:09
source share

In netbeans 8.1, I did to include a folder of icons and other images called Resources inside the src folder in the project file. Therefore, whenever I create a Jar file, the folder will be added there. The file tree should be like this:

  • src (Java files in source packages here)
  • ** PACKAGE YOU ARE IN THE PROJECT **

    • file.java
  • Resources

    • image.jpg

The code should look like this:

 jToggleButton1.setIcon(new javax.swing.ImageIcon(this.getClass().getResource("/resources/image.jpg"))); 
+1
Feb 07 '17 at 8:21
source share

Downloading an image from a Jar file at runtime is the same as loading an image when executing from an IDE, such as netbeans, the difference is that when loading an image from a JAR file, the path must be correct and case sensitive (very important). It 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 lowercase, it will not work. checking spaces, cases and spelling

0
Dec 07 '15 at 16:10
source share



All Articles