java 尝试从 jar 文件加载图标
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1133066/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Trying to load icon from jar file
提问by
I am trying to load icons from a jar file. I have both tried to load it from classes within the jar file as well as classes outside the jar file.
我正在尝试从 jar 文件加载图标。我都尝试从 jar 文件内的类以及 jar 文件外的类加载它。
outside of the jarfile - returned a null exception
在 jarfile 之外 - 返回空异常
java.net.URL imageURL = LoadHTMLExample.class.getClassLoader()
.getResource("icons/mouse.png");
in side of the jar file in the LoadHTMLExample
在 LoadHTMLExample 中 jar 文件的一侧
java.net.URL imageURL = this.getClass().getClassLoader()
.getResource("icons/mouse.png");
get the same error.
得到同样的错误。
I have also tried variations of "icons", "/icons" "icons/" "/icons/mouse.png" "icons/mouse.png"
我还尝试了“图标”、“/icons”、“icons/”、“/icons/mouse.png”、“icons/mouse.png”的变体
nothing seems to work any idea
任何想法似乎都不起作用
the icon is in the jar file
图标在 jar 文件中
jar
--icons --- {all the images}
--com.blah.blah
回答by Steve Kuo
I've always used the system class loader, whose path is relative to the root of the JAR:
我一直使用系统类加载器,它的路径是相对于 JAR 的根目录:
URL url = ClassLoader.getSystemClassLoader().getResource("icons/mouse.png");
Icon icon = new ImageIcon(url);
回答by banjollity
Skip the class loader, and get the resource as a stream instead. If you don't need the URL you can turn them directly into BufferedImages like so. I've left the stream and exception handling as a further exercise.
跳过类加载器,而是以流的形式获取资源。如果您不需要 URL,您可以像这样直接将它们转换为 BufferedImages。我将流和异常处理留作进一步练习。
InputStream stream = LoadHTMLExample.class
.getResourceAsStream( "/icons/mouse.png" );
BufferedImage image = ImageIO.read( stream );
Questioner needs the URL which brings us back to everyone else's suggestions. The images are definitely in the jar aren't they?
发问者需要一个 URL,它可以让我们回到其他人的建议。图像肯定在罐子里,不是吗?
回答by notnoop
I think that getResourcegets the resource relative to the location of LoadHTMLExample.class. So your jarfileshould be structured in the following way:
我认为getResource获取相对于LoadHTMLExample.class. 所以你的jarfile结构应该如下:
myjar.jar
|
|- ...
|- LoadHTMLExample.class
|- ...
\-- icons
|
\- mourse.png
Also, you might be getting stream through getResourceAsStreamthan getting the URL.
此外,您可能会获得流式传输而getResourceAsStream不是URL.
回答by banjollity
Is the jar in question on the classpath of your runtime? I have a jar with PNGs in it, and I can recreate the nulls if I don't include it on the classpath. If the jar is there they go away.
jar 是否在运行时的类路径中?我有一个带有 PNG 的 jar,如果我不在类路径中包含它,我可以重新创建空值。如果罐子在那里,它们就会消失。
回答by banjollity
final java.net.URL imageURL3 = com.java.html.LoadHTMLExample.class.getResource( "icons/" );
最终 java.net.URL imageURL3 = com.java.html.LoadHTMLExample.class.getResource("icons/");
works for the below directory structure
适用于以下目录结构
myjar.jar | |- ... |- LoadHTMLExample.class |- ... -- icons | - mourse.png
myjar.jar | |- ... |- LoadHTMLExample.class |- ... -- 图标 | - 摩尔斯.png
Thanks for the help everyone
感谢大家的帮助
回答by Adam Taras
Icon icon = new ImageIcon(ClassLoader.getSystemResource("icons/mouse.png"));
From the JavaDocof getSystemResource():
来自JavaDoc的getSystemResource():
Find a resource of the specified name from the search path used to load classes...
从用于加载类的搜索路径中查找指定名称的资源...
This will find your icons even in a jar-file.
即使在 jar 文件中,这也会找到您的图标。

