Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
286 views
in Technique[技术] by (71.8m points)

java - Eclipse exported Runnable JAR not showing images

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?

question from:https://stackoverflow.com/questions/25635636/eclipse-exported-runnable-jar-not-showing-images

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...