java 将图像添加到 jar
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5485034/
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
Add images to jar
提问by maks
I want to set icon to my JFrame. I do the following:
我想为我的 JFrame 设置图标。我执行以下操作:
Image icon = Toolkit.getDefaultToolkit().getImage("src/images/icon.jpg");
this.setIconImage(icon);
It works fine when I run this code from netbeans, but when I try to run this code from jar file, images are not shown in my JFrame. I have tried to load images as resources:
当我从 netbeans 运行此代码时它工作正常,但是当我尝试从 jar 文件运行此代码时,图像未显示在我的 JFrame 中。我试图将图像加载为资源:
this.setIconImage(Toolkit.getDefaultToolkit().getImage(getClass().getResource("/src/images/icon.jpg")));
but when I run this code it fails with NullPointerException
但是当我运行这段代码时它失败了 NullPointerException
Uncaught error fetching image:
java.lang.NullPointerException
at sun.awt.image.URLImageSource.getConnection(URLImageSource.java:99)
at sun.awt.image.URLImageSource.getDecoder(URLImageSource.java:113)
at sun.awt.image.InputStreamImageSource.doFetch(InputStreamImageSource.java:240)
at sun.awt.image.ImageFetcher.fetchloop(ImageFetcher.java:172)
at sun.awt.image.ImageFetcher.run(ImageFetcher.java:136)
How can I do this work?
我怎样才能完成这项工作?
edit:Thanks to all, the problem was solved by specifying image as
编辑:感谢所有人,通过将图像指定为解决了问题
Toolkit.getDefaultToolkit().getImage(getClass().getClassLoader().getResource("images/icon.JPG"))
As for it seems rather weird, and would be better if it was like
至于好像比较奇怪,要是像这样就好了
this.setIconImage(new ImageIcon(pathToIcon).getImage());
采纳答案by Stephen C
Looking at the source code of URLImageSource, it appears that the reason that getConnection
throws an NPE is that it has a null
for the url. And that leads me to suspect that
查看URLImageSource的源代码,似乎getConnection
抛出NPE的原因是它对null
url有一个。这让我怀疑
getClass().getResource("/src/images/icon.jpg")
is returning null
. It would do that if it could not locate a resource with that pathname.
正在返回null
。如果它找不到具有该路径名的资源,它就会这样做。
I bet that the problem is that you've got the path wrong.
我敢打赌,问题是你走错了路。
To prove / disprove this, you should run jar tvf
on the JAR file, and look for the line that matches "icon.jpg"
. Is the corresponding pathname the same as what you are using? If not, use the pathname from the matching line in the getResource
call and it should work. Alternatively, if the file doesn't show up at all, look at the NetBeans build configs that tell it what to put in the JAR file. (I'm not a NetBeans user, so I can't say where you would need to look ...)
要证明/反驳这一点,您应该运行jar tvf
JAR 文件,并查找匹配"icon.jpg"
. 对应的路径名是否与您使用的相同?如果没有,请在getResource
调用中使用匹配行中的路径名,它应该可以工作。或者,如果该文件根本没有显示,请查看 NetBeans 构建配置,该配置告诉它在 JAR 文件中放入什么内容。(我不是 NetBeans 用户,所以我不能说您需要在哪里查看...)
If that leads you absolutely nowhere, another possibility is that getClass().getResource(...)
is using a classloader that doesn't know about the JAR file containing the image. (This seems pretty improbable to me ...)
如果这让您无处可去,另一种可能性是getClass().getResource(...)
使用了一个不知道包含图像的 JAR 文件的类加载器。(这对我来说似乎不太可能......)
回答by Sanjay T. Sharma
Assuming your JAR file has a top level directory called images
, you can use either:
假设您的 JAR 文件有一个名为 的顶级目录images
,您可以使用:
getClass().getResource("/images/icon.jpg")
orgetClass().getClassLoader().getResource("images/icon.jpg")
getClass().getResource("/images/icon.jpg")
或者getClass().getClassLoader().getResource("images/icon.jpg")
回答by Konstantin Burov
getResource()
loads a resource from classpath, not an OS path, and the after compilation your classpath will not include the /src
folder, but rather just its contents. So you'd better try /images/icon.jpg
.
getResource()
从类路径而不是操作系统路径加载资源,编译后您的类路径将不包含该/src
文件夹,而仅包含其内容。所以你最好试试/images/icon.jpg
。
Also you may find this discussionsomewhat useful.
此外,您可能会发现此讨论有些有用。
回答by Andrew Thompson
.."/src/images/icon.jpg"..
The '/src' prefix of the address seems suspicious. Many apps. will provide separate 'src' and 'build' directories, but it normally ends up that the 'src' prefix is not used in the resulting Jar. I recommend trying..
地址的“/src”前缀似乎很可疑。许多应用程序。将提供单独的 'src' 和 'build' 目录,但通常最终结果是在生成的 Jar 中不使用 'src' 前缀。我建议尝试..
.."/images/icon.jpg"..
& also triplechecking that the image is in the location of the Jar that the code is expecting to find it.
以及三重检查图像是否位于代码期望找到它的 Jar 的位置。
回答by jberg
This should do it assuming you can import javax.imageio.ImageIO:
假设您可以导入 javax.imageio.ImageIO,则应该这样做:
Image icon = ImageIO.read(this.getClass().getResource("/src/images/icon.jpg"));
this.setIconImage(icon);
回答by Gabriel Ozzy
You can simply create a package inside the main source, and incluse your images in this package. Then, just call the images in your main class like:
您可以简单地在主源中创建一个包,并将您的图像包含在该包中。然后,只需在主类中调用图像,例如:
ImageIcon a = new ImageIcon(MainClass.class.getResource("/Package/Image.jpg"));
ImageIcon a = new ImageIcon(MainClass.class.getResource("/Package/Image.jpg"));
回答by Peter Moh
JFrame f = new JFrame("Edit Configure File");
//Image image = ImageIO.read(getClass().getResource("images/ctx.Icon"));
f.setIconImage(new ImageIcon("images/ctx.PNG").getImage());//this works for me finally
//f.setIconImage(image);
//f.setIconImage(Toolkit.getDefaultToolkit().getImage(getClass().getResource("images/ctx.PNG")));
回答by Dipmedeep
For this to work, you should access the images from a directory relative to some fixed class.
For example, if the image files are saved in a directory "images" on the same level as the Toolkit.class
, then
为此,您应该从相对于某个固定类的目录访问图像。例如,如果图像文件保存在与 相同级别的目录“images”中Toolkit.class
,则
this.setIconImage(Toolkit.getDefaultToolkit().getImage(Toolkit.class.getResource("images/icon.jpg")));
should work.
this.setIconImage(Toolkit.getDefaultToolkit().getImage(Toolkit.class.getResource("images/icon.jpg")));
应该管用。