简体   繁体   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

I'm working on a Java program that uses icon files. 我正在使用图标文件的Java程序。 The icons are stored in a folder Icons in /src . 图标存储在/src中的文件夹Icons中。 The code to access an icon is 访问图标的代码是

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

The program runs properly in Eclipse. 该程序可以在Eclipse中正常运行。 But when I export its runnable jar to a folder abc , then it needs the Icons folder to be present in abc . 但是,当我将其可运行jar导出到文件夹abc ,它需要将Icons文件夹存在于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? 要对代码进行什么更改,以使jar本身而不是在其所在的文件夹中查找图标文件?

Try this: 尝试这个:

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

This works fine for me. 这对我来说很好。

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

相关问题 classLoader.getResource在jar文件中不起作用 - classLoader.getResource doesn't work in jar file ClassLoader.getResource()在.jar文件中不起作用 - ClassLoader.getResource() doesn't work from .jar file Class.getResource()和ClassLoader.getResource()在可执行jar中的奇怪行为 - Strange behavior of Class.getResource() and ClassLoader.getResource() in executable jar classLoader.getResource(“。”)是/ bin文件夹 - classLoader.getResource(“.”) is /bin folder NullPointerException on ClassLoader.getResource() - NullPointerException on ClassLoader.getResource() 在NetBeans中创建jar文件后,ImageIcon图标=新的ImageIcon(getClass()。getResource(String))不起作用 - ImageIcon icon = new ImageIcon(getClass().getResource(String)) doesn't work after creating jar file in netbeans 使用ClassLoader.getResource()加载BufferedImage - loading BufferedImage with ClassLoader.getResource() Apache CLI,可执行jar,classLoader()。getResource() - Apache CLI, Executable jar, classLoader().getResource() classloader.getResource返回带有war文件的路径 - classloader.getResource return a path with war file java class getResource 找不到 jar 文件中列出的图标 - java class getResource can't find icons listed in jar file
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM