简体   繁体   中英

Image not found when running as Jar

I want to ask about a problem in my project: I have a class that extends JFrame , and I want to change the Java icon on that frame to another icon using this code:

setIconImage(new ImageIcon(getClass().getResource("icon.PNG")).getImage());

I'm using NetBeans , and when I run my project from NetBeans, it runs normally, but when I run it from the jar, it does not run. Then, when I remove that code, my jar runs normally with the Java icon in the frame.

Can anyone can tell me what might be wrong with that code?

When running jar file you cannot access files by using:

getClass().getResource("...")

You should use:

getClass().getResourceAsStream("...")

See: File loading by getClass().getResource()

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