Java Eclipse 导出的 Runnable JAR 未显示图像
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/25635636/
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
Eclipse exported Runnable JAR not showing images
提问by DeNitE Appz
My images will not load when running a JAR file exported from Eclipse.
运行从 Eclipse 导出的 JAR 文件时,我的图像不会加载。
I have the images in a resources class package. I've tried a images source folder as well with no luck.
我在资源类包中有图像。我也尝试过图像源文件夹,但没有运气。
Works perfectly when loaded from Eclipse. The images are in the exported JAR file, so they're exporting fine.
从 Eclipse 加载时完美运行。图像位于导出的 JAR 文件中,因此它们可以正常导出。
I've tried:
我试过了:
label.setIcon(new ImageIcon(MainFrame.class.getResource("/resources/header.jpg")));
I've also tried:
我也试过:
URL url = getClass().getResource("/resources/header.jpg");
Image image = Toolkit.getDefaultToolkit().getImage(url);
label.setIcon(new ImageIcon(image));
And:
和:
try
{
label.setIcon(new ImageIcon(ImageIO.read(getClass().getResource("/resources/header.jpg"))));
}
catch (IOException e1)
{
e1.printStackTrace();
}
Any suggestions?
有什么建议?
采纳答案by DeNitE Appz
The problem was I had this project in my Windows profile... that had an "!" in it... (DeNitE! -> was the name of my Windows profile)
问题是我的 Windows 配置文件中有这个项目......它有一个“!” 在其中...(DeNitE!-> 是我的 Windows 配置文件的名称)
As soon as I changed it to DeNitE (without the !) it worked fine...
一旦我将其更改为 DeNitE(没有!),它就可以正常工作......
回答by Paul Samsotha
Works fine for me. Check what you may have different.
对我来说很好用。检查您可能有什么不同。
Example 1: (resources insrc)
实施例1:(资源在SRC)
Steps:
脚步:
File Structure
Code
package com.stackoverflow.test; import java.net.URL; import javax.swing.*; // Wild carded for brevity. // Actual code imports single classes public class Main { public static void main(String[] args) { SwingUtilities.invokeLater(new Runnable(){ public void run() { URL url = Main.class.getResource( "/resources/stackoverflow.png"); ImageIcon icon = new ImageIcon(url); JFrame frame = new JFrame(); frame.add(new JLabel(icon)); frame.pack(); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setLocationRelativeTo(null); frame.setVisible(true); } }); } }
[Right click on project] → [Export] → [Runnable Jar File] → [Set up Launch config]
Profit
文件结构
代码
package com.stackoverflow.test; import java.net.URL; import javax.swing.*; // Wild carded for brevity. // Actual code imports single classes public class Main { public static void main(String[] args) { SwingUtilities.invokeLater(new Runnable(){ public void run() { URL url = Main.class.getResource( "/resources/stackoverflow.png"); ImageIcon icon = new ImageIcon(url); JFrame frame = new JFrame(); frame.add(new JLabel(icon)); frame.pack(); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setLocationRelativeTo(null); frame.setVisible(true); } }); } }
【右键项目】→【导出】→【Runnable Jar File】→【设置启动配置】
利润
FYI, the same setup runs in eclipse just fine also
仅供参考,同样的设置在 eclipse 中运行也很好
Example 2: (resources notin src - but in project)
示例 2:(资源不在src - 但在项目中)
Steps:
脚步:
File Structure (notice resources looks like a plain folder)
What we have to do now, is put the resources on the build path. What this does is put everything in the folder (excluding the folder itself) on the classpath
Right click on the project and go to [Build Path] → [Configure Build Path]
From the [Sources] tab in the dialog, select [Add Folder] and in the new dialog, select the [resources] folder
Now the contentsof the resources folder is in the build path (notice the little package in the folder now
New code no longer uses the resources prefix for the path
URL url = Main.class.getResource("/stackoverflow.png");
Same as Step 3 and 4 from above, and profit!
文件结构(注意资源看起来像一个普通的文件夹)
我们现在要做的就是将资源放在构建路径上。这样做是将所有内容放在类路径上的文件夹中(不包括文件夹本身)
右击项目,进入【构建路径】→【配置构建路径】
从对话框的 [Sources] 选项卡中,选择 [Add Folder],然后在新对话框中选择 [resources] 文件夹
现在资源文件夹的内容在构建路径中(注意现在文件夹中的小包
新代码不再使用资源前缀作为路径
URL url = Main.class.getResource("/stackoverflow.png");
与上面的第3和第4步相同,并获利!
UPDATE
更新
Setting up Launch Configuration
设置启动配置
Generally, once you run the class (i.e. Right click on class and Run as Java Application), a run configuration will be set up. You will need this to set as the launching point in the manifest. But here's how to do it manually.
通常,一旦您运行该类(即右键单击该类并作为 Java 应用程序运行),就会设置一个运行配置。您需要将其设置为清单中的启动点。但这是手动操作的方法。
Steps:
脚步:
[Right Click Project] → [Properties] → [Run/Debug Settings]
You can see that I already have a run configruation (that is implicitly set from simply running the class). But to create a new one, select [New] → [Java Application]
Create a name for run configuration and browse or type an main launching class. In my case its the
com.stackoverflow.test.Main
classNow when you export as shown in the above example, you select the run configuration
Run the jar like above.
【右键项目】→【属性】→【运行/调试设置】
你可以看到我已经有了一个运行配置(这是通过简单地运行类隐式设置的)。但是要新建一个,选择[New] → [Java Application]
创建运行配置的名称并浏览或键入主要启动类。在我的情况下它的
com.stackoverflow.test.Main
类现在,当您如上例所示导出时,您选择运行配置
像上面一样运行jar。
EDIT
编辑
Result to Check for
检查结果
Manifest:
显现:
Manifest-Version: 1.0
Rsrc-Class-Path: ./
Class-Path: .
Rsrc-Main-Class: com.stackoverflow.test.Main
Main-Class: org.eclipse.jdt.internal.jarinjarloader.JarRsrcLoader
Extracted jar:
提取的罐子:
回答by phchen2
For creating a runnable JAR file from Eclipse we may refer to the article "Creating Runnable Jars in Eclipse (or other IDE...or by hand):" (https://www.cefns.nau.edu/~edo/Classes/CS477_WWW/Docs/RunnableJarsinEclipse.html), it mentioned that we need do four things as
要从 Eclipse 创建可运行的 JAR 文件,我们可以参考文章“在 Eclipse(或其他 IDE...或手动)中创建可运行的 JAR”:(https://www.cefns.nau.edu/~edo/Classes /CS477_WWW/Docs/RunnableJarsinEclipse.html),它提到我们需要做四件事
- Make sure create a package for our code, not just create a project in Eclipse
- Create the sub-pakage (subfolder) for our resource files under the main package of our code (note that the sub-package is under main package, is not only in the project)
- get all file references from getResource() (getting the URL reference)
- Export of files as a runnable JAR in Eclipse (File -> Export... -> select Runnable JAR files -> next -> ...)
- 确保为我们的代码创建一个包,而不仅仅是在 Eclipse 中创建一个项目
- 在我们的代码的主包下为我们的资源文件创建子包(子文件夹)(注意子包在主包下,不只是在项目中)
- 从 getResource() 获取所有文件引用(获取 URL 引用)
- 在 Eclipse 中将文件导出为可运行的 JAR(文件 -> 导出... -> 选择可运行的 JAR 文件 -> 下一个 -> ...)
But for image file in the example code of above article it only creates the ImageIcon, it does not create the SWT Image, and there are many questions in the Internet for how to get SWT Image from URL or how to convert ImageIcon to SWT Image, below is the example code for getting the SWT Image from URL,
但是对于上面文章示例代码中的图像文件,它只创建了ImageIcon,并没有创建SWT Image,而且网上有很多关于如何从URL获取SWT Image或如何将ImageIcon转换为SWT Image的问题,下面是从 URL 获取 SWT 图像的示例代码,
Image imgSWT=null; // Image class is the SWT Image class
ImageDescriptor imgDesc=null;
java.net.URL imgURL = YourClassName.class.getResource("path/image_filename");
if (imgURL != null) {
imgDesc = ImageDescriptor.createFromURL(imgURL);
imgSWT = imgDesc.createImage();
}
回答by sazzy4o
回答by TheChubbyPanda
Unless you have to have you files in the jar, this is probably the simplest way of doing it:
除非您必须将文件放在 jar 中,否则这可能是最简单的方法:
header = ImageIO.read(new File("./resources/header.jpeg"));
header = ImageIO.read(new File("./resources/header.jpeg"));
header has to be an Image/BufferedImage. This goes to the folder that the runnable jar is within and looks for a folder called resources. http://i.stack.imgur.com/x8xtO.png
标头必须是 Image/BufferedImage。这将转到可运行 jar 所在的文件夹,并查找名为资源的文件夹。 http://i.stack.imgur.com/x8xtO.png
回答by Michel Fernandes
Two Simple steps:
两个简单的步骤:
1 - Add the folder to Build Path;
1 - 将文件夹添加到构建路径;
2 - Use this:
2 - 使用这个:
InputStream url = this.getClass().getResourceAsStream("/load04.gif");
myImageView.setImage(new Image(url));
回答by Nevets17
Another work around is if you put your resource file, an your .jar file in the same location, it should work. The only drawback is you have to have the resources with the jar file at all times.
另一种解决方法是,如果您将资源文件(一个 .jar 文件)放在同一位置,它应该可以工作。唯一的缺点是您必须始终拥有 jar 文件的资源。
If that's not an issue you can do it this way.
如果这不是问题,您可以这样做。
回答by Md. Sakib Hossain
I have same issue.. You can use this..
我有同样的问题..你可以使用这个..
Image Icon= new ImageIcon(new WE4().getClass().getResource("IC.jpg"))
Here WE4 is my class constructor name. Hope it helps.
这里 WE4 是我的类构造函数名称。希望能帮助到你。