Java 图像未出现在 JAR 文件中

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19504745/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 17:49:13  来源:igfitidea点击:

Java images not appearing in JAR file

javaimageembedded-resource

提问by user237462

Heres how i call the image

这是我如何调用图像

Icon logoOne = new ImageIcon("logo.png");
// LOGO
JLabel imageLogo = new JLabel(logoOne); 
imageLogo.setPreferredSize(new Dimension(200, 50));

Its just added to a panel in a JFrame and works fine when running in eclipse but doesnt show up when exported to JAR.

它刚刚添加到 JFrame 的面板中,在 eclipse 中运行时工作正常,但在导出到 JAR 时不显示。

can anyone help ?

有人可以帮忙吗?

采纳答案by Tim Visée

When exporting your JAR file, you've to make sure you're also including the image files in your JAR file. For example: If you have a folder called 'res' containing the images, you have to check the checkbox before this folder in order to make sure this folder, and the images are being included.

导出 JAR 文件时,您必须确保在 JAR 文件中还包含图像文件。例如:如果您有一个包含图像的名为“res”的文件夹,则必须选中该文件夹前的复选框以确保该文件夹和图像被包含在内。

You might want to add some trouble shooting code. For example: Add some code that prints in the console if this image file exists. Export your JAR file, run your JAR via the console/terminal and check the result. If this shows you the image doesn't exists, you know it's a problem with your exported JAR file.

您可能想要添加一些故障排除代码。例如:如果此图像文件存在,则添加一些在控制台中打印的代码。导出 JAR 文件,通过控制台/终端运行 JAR 并检查结果。如果这显示图像不存在,您就知道导出的 JAR 文件有问题。

You might want to try reaching the image as a resource steam, give the following code sample a try:

您可能想尝试将图像作为资源流访问,请尝试以下代码示例:

java.net.URL logoOneUrl = getClass().getResource("logo.png");
Icon logoOne = new ImageIcon(logoOneUrl );

回答by Hovercraft Full Of Eels

Don't get your image as a file as you're currently doing so. Instead get it as a resource. If you search this site, you'll find similar questions about occurring 2-4 times per week.

不要像当前那样将图像作为文件获取。而是将其作为资源获取。如果您搜索该网站,您会发现类似的问题每周会发生 2-4 次。

i.e.,

IE,

String imageResource = //.... string to image resource path
Image myImage = ImageIO.read(getClass().getResourceAsStream(imageResource));
Icon logo = new ImageIcon(myImage);

Note that the imageResource String is the String that represents the resource path to your image. This will depend on where your image is held in the jar file and where it is relative to the class files, information that we don't know at the moment.

请注意,imageResource 字符串是表示图像资源路径的字符串。这将取决于您的图像在 jar 文件中的位置以及它相对于类文件的位置,我们目前不知道这些信息。

回答by Matthew Farwell

When you call

你打电话时

Icon logoOne = new ImageIcon("logo.png");

You are reading the image from a file (see javadoc). When you export to the jar, this file is no longer available, so you need to use ClassLoader#getResourceFromClasspath() or use one of the other constructors.

您正在从文件中读取图像(请参阅javadoc)。当您导出到 jar 时,此文件不再可用,因此您需要使用 ClassLoader#getResourceFromClasspath() 或使用其他构造函数之一。

回答by Simiil

That depends on how you added the Image to the jar file.

这取决于您如何将图像添加到 jar 文件中。

In the normal case, the image lies in one of your source folders, and eclipse takes care of exporting it into the Jar file. If that is the case, you can load the via the ClassLoader. there are several methods for that. I mostly use this.getClass().getResourceAsStream("logo.png"). If you only use the standard Classloader (which would be the normal case) this will work.

在正常情况下,图像位于您的源文件夹之一中,eclipse 负责将其导出到 Jar 文件中。如果是这种情况,您可以通过ClassLoader. 有几种方法可以做到这一点。我主要使用this.getClass().getResourceAsStream("logo.png"). 如果您只使用标准类加载器(这是正常情况),这将起作用。