Add image to JAR Java
Asked Answered
T

7

24

I'm using some images in JFrame, I have given a path to load image into panel of the frame, but when i made jar of the application its not displaying images.
Where should I place the image?
How should I specify the path?

setIconImage(Toolkit.getDefaultToolkit().getImage(
                "D:\\RFT\\src\\dailogBox\\dump.jpg"));

like this i have done.

Tarrance answered 8/7, 2009 at 6:29 Comment(0)
T
46

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.

Second step: Access the file from your program. I assume you put it in the package (the path in your JAR) package1/package2 and the file is called dump.jpg. In that case you call:

setIconImage(Toolkit.getDefaultToolkit().getImage(getClass().getResource("/package1/package2/dump.jpg")));

getClass().getResource(...) returns an URL for a resource on your classpath. You start the path in the classpath with a '/' and use the complete path/packages to your resource, separated by '/'.

Thalassography answered 8/7, 2009 at 6:55 Comment(1)
@KareemMesbah, you would just reference it as MyClass.class.getResource("/path/to/file.jpg")Slight
T
7

This IS the best way to handle all images and icons in a JAR App.

Once you've zipped up all of your images and icons into its own JAR file - Configure your build path by adding the images JAR file into your libraries tab so that its now included in your classpath.

Then simply use the following 3x lines of code at the start of your constuctor to access any image you need for anything including a SystemTray image which doesn't accept the simple ImageIcon's as its main icon (weird I know). The 3x lines are:

  URL iconUrl = this.getClass().getResource("/image-iconb.png");
  Toolkit tk = this.getToolkit();
  someimgicon = tk.getImage(iconUrl);

(someimgicon is just a constructor declared Image variable) Now you can set a window icon as simply as:

setIconImage(someimgicon);

and at the same time use the same variable when setting the System TrayIcon by declaring:

 trayIcon = new TrayIcon(someimgicon, "SystemTray Demo", popupMenu);

The above allows you to declare Images or ImageIcons easily and centrally without running the risk of not keeping image resources in the right place. It keeps it nice and tidy, with the JAR containing all your images automatically compiled at run time and distribution of your program.

As a bonus, once the JAR is registered in your classpath - you can keep adding any other images into the same JAR at any time without any fuss too - Everything just works and the added images are instantly available to your app.

Much better in my view.

Torrefy answered 30/12, 2014 at 22:58 Comment(1)
We should zip ALL of our images and icons into it's own JAR file, and then have our IDE make another JAR file which encapsulates the original JAR file? What?! WhySubversion
C
3

First Create a package(Say images) under Source Packages

copy all of your images to this package(when you create a package,a folder in the name of your package will be created inside your project src folder, so copy images to it)

You can access your Images from your program as

Example 1:

        this.setIconImage(Toolkit.getDefaultToolkit().getImage(getClass().getResource("/images/yourimagename")));

Example 2:

        URL imageurl = getClass().getResource("/images/imagename");//assuming your package name is images 
        Image myPicture = Toolkit.getDefaultToolkit().getImage(imageurl);
        JLabel piclabel = new JLabel(new ImageIcon( myPicture ));
        piclabel.setBounds(0,0,myPicture.getWidth(null),myPicture.getHeight(null));

Now use this JLabel piclabel

Contexture answered 22/8, 2012 at 3:4 Comment(0)
I
1

There are many ways, for example make a folder called resources in your jar file and reference your images from there using a relative path....

Indue answered 8/7, 2009 at 6:31 Comment(1)
i have pasted image inside the source folder,how to set relative path?Tarrance
M
1

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")));
Margrettmarguerie answered 7/2, 2017 at 9:14 Comment(0)
K
0

Add the image in your jar as a resource, then (assuming the jar is on your classpath), use the class loader to load it as a stream.

Keene answered 8/7, 2009 at 6:34 Comment(2)
can u give me an example for this?Tarrance
getClass().getResourceAsStream("/icons/closed_folder_small.png");Sustentacular
W
-2

The easy way to do this is to have the folder your image is in parallel to the jar file. Example:

Say in the code you call the image like this:

pic = new ImageIcon("Pics/buttonpic.jpg");

This means your picture would be called buttonpic and is located in a folder called Pics in your project directory. Now after you make your jar file, copy that Pics folder into the same folder your jar file is located.

Example: if your jar file is located on your desktop, then put the Pics folder on the desktop. Now when you run the jar file, your pictures will work correctly. Now to cover up the fact that you need to have your Pics folder next to your jar file, you can create a new folder and place in that folder your jar and the Pics folder. Then right click the jar and create a shortcut, then place that shortcut on the desktop or where you wanted to have just the jar file, and place the new folder you created wherever you want to hide it at.

Wooley answered 9/8, 2010 at 15:36 Comment(1)
I think that You got those downvotes becouse preffered way is to store all your assets in JAR archive with application. That way, you have to distribute only one file.Malindamalinde

© 2022 - 2024 — McMap. All rights reserved.