简体   繁体   中英

Exporting Java Project from Eclipse, images are not shown

I have an Eclipse Java project with about 10 external jar dependencies. It works fine in Eclipse, also I export it using tools like ONE-JAR and it works fine.

The strange problem I am facing is with images. I have all images in a package inside the project, and when exported they are included but they do not show in the running project?

I have no idea why? Strangely, in some JPanel text, I use HTML with embedded images using eg:

URL url = myClass.class.getResource( "icons/icon.png");

and this works and shows those images at output, but the ones directly referenced by Java, eg:

this.setIconImage(Toolkit.getDefaultToolkit().getImage("src/icons/icon.png"));

do not work. This same line of code works perfectly when running from Eclipse. Can you please suggest what could be the problem, or what should I do while exporting this project. Should I switch to Ant or Maven???

You should do this:

URL url = myClass.class.getResource( "icons/icon.png");
this.setIconImage(Toolkit.getDefaultToolkit().getImage(url));

Toolkit provides a method accepting a URL as well, see java.awt.Toolkit#getImage() .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM