Eclipse exported Runnable JAR not showing images
Asked Answered
G

8

39

My images will not load when running a JAR file exported from Eclipse.

I have the images in a resources class package. I've tried a images source folder as well with no luck.

Works perfectly when loaded from Eclipse. The images are in the exported JAR file, so they're exporting fine.

I've tried:

label.setIcon(new ImageIcon(MainFrame.class.getResource("/resources/header.jpg")));

I've also tried:

URL url = getClass().getResource("/resources/header.jpg");
Image image = Toolkit.getDefaultToolkit().getImage(url);
label.setIcon(new ImageIcon(image));

And:

try
{
    label.setIcon(new  ImageIcon(ImageIO.read(getClass().getResource("/resources/header.jpg"))));
}
catch (IOException e1)
{
    e1.printStackTrace();
}

Any suggestions?

Gentlemanly answered 3/9, 2014 at 3:8 Comment(3)
remove /resources from getClass().getResource("/resources/header.jpg"); because whe you build application's JAR there ll be no /resources directory.Acnode
Was resources included in the build path?Sasha
Yes, they are in the build path and are in the JAR when viewing it's contents...Gentlemanly
G
0

The problem was I had this project in my Windows profile... that had an "!" in it... (DeNitE! -> was the name of my Windows profile)

As soon as I changed it to DeNitE (without the !) it worked fine...

Gentlemanly answered 15/11, 2015 at 22:24 Comment(0)
S
88

Works fine for me. Check what you may have different.

Example 1: (resources in src)

Steps:

  1. File Structure

    enter image description here

  2. Code

    package com.stackoverflow.test;
    
    import java.net.URL;
    import javax.swing.*;  // Wild carded for brevity. 
                           // Actual code imports single classes
    public class Main {
        public static void main(String[] args) {
            SwingUtilities.invokeLater(new Runnable(){
                public void run() {
                    URL url = Main.class.getResource(
                                         "/resources/stackoverflow.png");
                    ImageIcon icon = new ImageIcon(url);
                    JFrame frame = new JFrame();
                    frame.add(new JLabel(icon));
                    frame.pack();
                    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                    frame.setLocationRelativeTo(null);
                    frame.setVisible(true);
                }
            });
        }
    }
    
  3. [Right click on project] → [Export] → [Runnable Jar File] → [Set up Launch config]

    enter image description here

  4. Profit

    enter image description here

FYI, the same setup runs in eclipse just fine also


Example 2: (resources not in src - but in project)

Steps:

  1. File Structure (notice resources looks like a plain folder)

    enter image description here

  2. What we have to do now, is put the resources on the build path. What this does is put everything in the folder (excluding the folder itself) on the classpath

    • Right click on the project and go to [Build Path] → [Configure Build Path]

      enter image description here

    • From the [Sources] tab in the dialog, select [Add Folder] and in the new dialog, select the [resources] folder

      enter image description here

    • Now the contents of the resources folder is in the build path (notice the little package in the folder now

      enter image description here

  3. New code no longer uses the resources prefix for the path

    URL url = Main.class.getResource("/stackoverflow.png");
    
  4. Same as Step 3 and 4 from above, and profit!


UPDATE

Setting up Launch Configuration

Generally, once you run the class (i.e. Right click on class and Run as Java Application), a run configuration will be set up. You will need this to set as the launching point in the manifest. But here's how to do it manually.

Steps:

  1. [Right Click Project] → [Properties] → [Run/Debug Settings]

    enter image description here You can see that I already have a run configruation (that is implicitly set from simply running the class). But to create a new one, select [New] → [Java Application]

  2. Create a name for run configuration and browse or type an main launching class. In my case its the com.stackoverflow.test.Main class

    enter image description here

  3. Now when you export as shown in the above example, you select the run configuration

    enter image description here

  4. Run the jar like above.


EDIT

Result to Check for

Manifest:

Manifest-Version: 1.0
Rsrc-Class-Path: ./
Class-Path: .
Rsrc-Main-Class: com.stackoverflow.test.Main
Main-Class: org.eclipse.jdt.internal.jarinjarloader.JarRsrcLoader

Extracted jar:

enter image description here

Saunders answered 3/9, 2014 at 4:11 Comment(3)
@peeskillet can you answer my question pls ? #38686037Russon
@peeskillet saved my day. I was exploring my codes for possible #bug. but realized this was the problemPolyphone
I've tried every solution you posted and in the end the one that worked was using this Launcher.class.getResource() where Launcher is my main.Kacey
M
2

I found that when you changed:

Package required libraries into generated jar

to

Extract required libraries into generated jar

in the Set up Launch config it worked for me.

Extract Required Libraries Button

Microcopy answered 12/3, 2016 at 17:44 Comment(0)
T
1

For creating a runnable JAR file from Eclipse we may refer to the article "Creating Runnable Jars in Eclipse (or other IDE...or by hand):" (https://www.cefns.nau.edu/~edo/Classes/CS477_WWW/Docs/RunnableJarsinEclipse.html), it mentioned that we need do four things as

  1. Make sure create a package for our code, not just create a project in Eclipse
  2. Create the sub-pakage (subfolder) for our resource files under the main package of our code (note that the sub-package is under main package, is not only in the project)
  3. get all file references from getResource() (getting the URL reference)
  4. Export of files as a runnable JAR in Eclipse (File -> Export... -> select Runnable JAR files -> next -> ...)

But for image file in the example code of above article it only creates the ImageIcon, it does not create the SWT Image, and there are many questions in the Internet for how to get SWT Image from URL or how to convert ImageIcon to SWT Image, below is the example code for getting the SWT Image from URL,

Image imgSWT=null;  // Image class is the SWT Image class
ImageDescriptor imgDesc=null;
java.net.URL imgURL = YourClassName.class.getResource("path/image_filename");

if (imgURL != null) {
    imgDesc = ImageDescriptor.createFromURL(imgURL);
    imgSWT = imgDesc.createImage();
}
Tuque answered 11/9, 2015 at 0:31 Comment(0)
P
1

Unless you have to have you files in the jar, this is probably the simplest way of doing it:

header = ImageIO.read(new File("./resources/header.jpeg"));

header has to be an Image/BufferedImage. This goes to the folder that the runnable jar is within and looks for a folder called resources. https://i.sstatic.net/x8xtO.png

Pero answered 1/6, 2016 at 19:48 Comment(0)
L
1

Two Simple steps:

1 - Add the folder to Build Path;

2 - Use this:

    InputStream url = this.getClass().getResourceAsStream("/load04.gif");
    myImageView.setImage(new Image(url));
Lepley answered 15/8, 2016 at 23:40 Comment(0)
F
1

I have same issue.. You can use this..

Image Icon= new ImageIcon(new WE4().getClass().getResource("IC.jpg"))

Here WE4 is my class constructor name. Hope it helps.

Frontlet answered 23/12, 2016 at 5:6 Comment(0)
G
0

The problem was I had this project in my Windows profile... that had an "!" in it... (DeNitE! -> was the name of my Windows profile)

As soon as I changed it to DeNitE (without the !) it worked fine...

Gentlemanly answered 15/11, 2015 at 22:24 Comment(0)
B
0

Another work around is if you put your resource file, an your .jar file in the same location, it should work. The only drawback is you have to have the resources with the jar file at all times.

If that's not an issue you can do it this way.

Battles answered 30/9, 2016 at 17:19 Comment(1)
No, this is false. Just tested this 5 minutes ago.Kory

© 2022 - 2024 — McMap. All rights reserved.