尝试从 jar 文件加载图标 [英] Trying to load icon from jar file
问题描述
我正在尝试从 jar 文件加载图标.我都尝试从 jar 文件内的类以及 jar 文件外的类加载它.
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.
在 jarfile 之外 - 返回空异常
outside of the jarfile - returned a null exception
java.net.URL imageURL = LoadHTMLExample.class.getClassLoader()
.getResource("icons/mouse.png");
在 LoadHTMLExample 中 jar 文件的一侧
in side of the jar file in the LoadHTMLExample
java.net.URL imageURL = this.getClass().getClassLoader()
.getResource("icons/mouse.png");
得到同样的错误.
我也尝试过"icons", "/icons" "icons/" "/icons/mouse.png" "icons/mouse.png"
I have also tried variations of "icons", "/icons" "icons/" "/icons/mouse.png" "icons/mouse.png"
似乎没有任何想法
图标在jar文件中
jar
--icons --- {all the images}
--com.blah.blah
推荐答案
跳过类加载器,而是以流的形式获取资源.如果您不需要 URL,您可以像这样直接将它们转换为 BufferedImages.我将流和异常处理留作进一步练习.
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.
InputStream stream = LoadHTMLExample.class
.getResourceAsStream( "/icons/mouse.png" );
BufferedImage image = ImageIO.read( stream );
<小时>
提问者需要一个 URL,它可以让我们回到其他人的建议.图像肯定在罐子里不是吗?
Questioner needs the URL which brings us back to everyone else's suggestions. The images are definitely in the jar aren't they?
这篇关于尝试从 jar 文件加载图标的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!