Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Add images to jar

Tags:

java

jar

I want to set icon to my JFrame. I do the following:

Image icon = Toolkit.getDefaultToolkit().getImage("src/images/icon.jpg");
this.setIconImage(icon);

It works fine when I run this code from netbeans, but when I try to run this code from jar file, images are not shown in my JFrame. I have tried to load images as resources:

this.setIconImage(Toolkit.getDefaultToolkit().getImage(getClass().getResource("/src/images/icon.jpg")));

but when I run this code it fails with NullPointerException

 Uncaught error fetching image:
java.lang.NullPointerException
        at sun.awt.image.URLImageSource.getConnection(URLImageSource.java:99)
        at sun.awt.image.URLImageSource.getDecoder(URLImageSource.java:113)
        at sun.awt.image.InputStreamImageSource.doFetch(InputStreamImageSource.java:240)
        at sun.awt.image.ImageFetcher.fetchloop(ImageFetcher.java:172)
        at sun.awt.image.ImageFetcher.run(ImageFetcher.java:136)

How can I do this work?

edit: Thanks to all, the problem was solved by specifying image as

Toolkit.getDefaultToolkit().getImage(getClass().getClassLoader().getResource("images/icon.JPG"))

As for it seems rather weird, and would be better if it was like

this.setIconImage(new ImageIcon(pathToIcon).getImage());
like image 683
maks Avatar asked Mar 30 '11 10:03

maks


People also ask

How do I add an image to a jar file?

First step: Put your image in a defined location in your JAR-file. If you put it into the src-folder, maybe your IDE or your build-tool will package it to the JAR automatically. Otherwise check the documentation of your IDE/build-tool, in which location you have to put it.

How do I add a resource file to a jar file?

1) click project -> properties -> Build Path -> Source -> Add Folder and select resources folder. 2) create your JAR! EDIT: you can make sure your JAR contains folder by inspecting it using 7zip. After following your EDIT tip, I learned that even though your resource file, e.g. someFile.

How do I create a JAR file with an external library?

If it is a standalone (Main method) java project then Not any specific path put all the jars inside the project not any specific path then right click on the project - > export - > Runnable jar --> Select the lunch configuration and Library handeling then choose the radio button option "Package required libraries into ...


2 Answers

Assuming your JAR file has a top level directory called images, you can use either:

  1. getClass().getResource("/images/icon.jpg") or
  2. getClass().getClassLoader().getResource("images/icon.jpg")
like image 83
Sanjay T. Sharma Avatar answered Sep 29 '22 09:09

Sanjay T. Sharma


Looking at the source code of URLImageSource, it appears that the reason that getConnection throws an NPE is that it has a null for the url. And that leads me to suspect that

getClass().getResource("/src/images/icon.jpg")

is returning null. It would do that if it could not locate a resource with that pathname.

I bet that the problem is that you've got the path wrong.

To prove / disprove this, you should run jar tvf on the JAR file, and look for the line that matches "icon.jpg". Is the corresponding pathname the same as what you are using? If not, use the pathname from the matching line in the getResource call and it should work. Alternatively, if the file doesn't show up at all, look at the NetBeans build configs that tell it what to put in the JAR file. (I'm not a NetBeans user, so I can't say where you would need to look ...)


If that leads you absolutely nowhere, another possibility is that getClass().getResource(...) is using a classloader that doesn't know about the JAR file containing the image. (This seems pretty improbable to me ...)

like image 44
Stephen C Avatar answered Sep 29 '22 08:09

Stephen C