简体   繁体   中英

ClassLoader.getResource() doesn't work for the icon files in a jar. The icons need to be present in the folder containing jar

I'm working on a Java program that uses icon files. The icons are stored in a folder Icons in /src . The code to access an icon is

image1 = new ImageIcon(Control.PrimaryGameFrame.class.getClassLoader().getResource("Icons/openFile.gif"));

The program runs properly in Eclipse. But when I export its runnable jar to a folder abc , then it needs the Icons folder to be present in abc . If I place the jar alone at a place, it doesn't load the icons. What changes are to be made to the code, for the jar to find the icon files in itself and not in the folder it is in?

Try this:

ImageIcon image1 = new ImageIcon(getClass.getResource("Icons/openFile.gif");

This works fine for me.

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