试图从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");
得到同样的错误。
I还尝试了
icons,/ iconsicons // icons / _mouse.pngicons / 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 );
提问者需要将我们带回给所有人的网址别的建议。绝对是罐子里的图像不是吗?
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屋!