繁体   English   中英

ClassLoader.getResource()对jar中的图标文件不起作用。 图标必须存在于包含jar的文件夹中

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

我正在使用图标文件的Java程序。 图标存储在/src中的文件夹Icons中。 访问图标的代码是

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

该程序可以在Eclipse中正常运行。 但是,当我将其可运行jar导出到文件夹abc ,它需要将Icons文件夹存在于abc 如果我将罐子放在一个地方,它不会加载图标。 要对代码进行什么更改,以使jar本身而不是在其所在的文件夹中查找图标文件?

尝试这个:

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

这对我来说很好。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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