Eclipse exported Runnable JAR not showing images

2018-12-31 20: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?

8条回答
裙下三千臣
2楼-- · 2018-12-31 21:13

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

查看更多
余生无你
3楼-- · 2018-12-31 21:20

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

查看更多
登录 后发表回答