Add Image to Jar Java

Add images to jar

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 ...)

How to bundle images in jar file

It seems like there are two questions here:

  1. How do I get NetBeans to include an image file in the jar produced when I build my project?

  2. How do I access an image file from a jar?

This answer applies to NetBeans 6.8 and addresses both of the subquestions.

Assume that you have an ant based Java Application Project.

Here is the 'Files' view of the project

JP
+ images
+ test.jpg
+ nbproject
+ src
+ jp
+ Main.java
+ test
+ build.xml
+ manifest.mf

Inside your Main.java you have code like this:

public static void main(String[] args) throws IOException {
// find the file in the file system.. probably not a good idea
File f = new File("images/test.jpg");
System.out.println(f.getCanonicalPath()+" "+f.exists());

When you run this project from inside NB you get this output:

/export/home/vkraemer/NetBeansProjects/JavaApplication2/images/test.jpg true

When you run the code packed into the jar, you get something like this:

bash-3.2$ pwd
/export/home/vkraemer/nbhg/web-main
bash-3.2$ java -jar /export/home/vkraemer/NetBeansProjects/JavaApplication2/dist/JavaApplication2.jar
/export/home/vkraemer/nbhg/web-main/images/test.txt false

To get something better when the jar is executed, you need to do the following:

Add the images directory as a source root for you project.

Right click on the project and select the Properties item. A dialog will appear.

Select 'Sources' in the list that is on the left side of the dialog. This will change the content of the panel on the right side of the dialog.

Press the 'Add Folder...' button that appears next to the 'Source Package Folders' table. A FileChooser will appear.

Use this chooser to select the images folder and press the OK button. An entry for the images folder will be added table.

Use the OK button on the Project Properties dialog to accept the changes and dismiss the dialog.

Change your code to use Class.getResource().

public static void main(String[] args) throws IOException {
// find the file in the file system.. probably not a good idea
File f = new File("images/test.jpg");
System.out.println(f.getCanonicalPath()+" "+f.exists());
URL url = Main.class.getResource("/test.jpg");
System.out.println(url);

When you run the project from inside the IDE, you should see something like this:

/export/home/vkraemer/NetBeansProjects/JavaApplication2/images/test.jpg true
file:/export/home/vkraemer/NetBeansProjects/JavaApplication2/images/test.jpg

When you run the code packed into the jar, you will get something like this:

bash-3.2$ pwd
/export/home/vkraemer/nbhg/web-main
bash-3.2$ java -jar /export/home/vkraemer/NetBeansProjects/JavaApplication2/dist/JavaApplication2.jar
/export/home/vkraemer/nbhg/web-main/images/test.jpg false
jar:file:/export/home/vkraemer/NetBeansProjects/JavaApplication2/dist/JavaApplication2.jar!/test.jpg

After you get the URL for the test.jpg file, you can use ImageIcon(URL) to create the icon

Java Swing: Displaying images from within a Jar

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.

Set Icon Image in Jar file

Use

this.getFrame().setIconImage(
new imageIcon(getClass().getClassLoader().getResource("PlagiaLyzerIcon.png"))
);

instead.

Note:

this line only works if the images are in the root of the jar file. If not, you have to specify the folder on the string:

getResource("yourfolder/PlagiaLyzerIcon.png")


Related Topics



Leave a reply



Submit