简体   繁体   English

Eclipse导出的Runnable JAR不显示图像

[英]Eclipse exported Runnable JAR not showing images

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. 从Eclipse加载时完美工作。 The images are in the exported JAR file, so they're exporting fine. 图像位于导出的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? 有什么建议么?

Works fine for me. 对我来说很好。 Check what you may have different. 检查您可能有什么不同。

Example 1: (resources in src) 实施例1:( SRC资源)

Steps: 脚步:

  1. File Structure 档案结构

    在此处输入图片说明

  2. 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); } }); } } 
  3. [Right click on project] → [Export] → [Runnable Jar File] → [Set up Launch config] [右键单击项目]→[导出]→[可运行的Jar文件]→[设置启动配置]

    在此处输入图片说明

  4. Profit 利润

    在此处输入图片说明

FYI, the same setup runs in eclipse just fine also 仅供参考,相同的设置在Eclipse中运行也很好


Example 2: (resources not in src - but in project) 示例2 :(资源不在 src中-而是在项目中)

Steps: 脚步:

  1. File Structure (notice resources looks like a plain folder) 文件结构(通知资源看起来像一个普通文件夹)

    在此处输入图片说明

  2. 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 contents of the resources folder is in the build path (notice the little package in the folder now 现在资源文件夹的内容在构建路径中(注意现在文件夹中的小包

      在此处输入图片说明

  3. New code no longer uses the resources prefix for the path 新代码不再使用资源前缀作为路径

     URL url = Main.class.getResource("/stackoverflow.png"); 
  4. Same as Step 3 and 4 from above, and profit! 从上面与第3步和第4步相同,并获利!


UPDATE 更新

Setting up Launch Configuration 设置启动配置

Generally, once you run the class (ie Right click on class and Run as Java Application), a run configuration will be set up. 通常,一旦您运行了该类(即右键单击该类并以Java应用程序身份运行),便会设置运行配置。 You will need this to set as the launching point in the manifest. 您需要将其设置为清单中的启动点。 But here's how to do it manually. 但是,这是手动操作的方法。

Steps: 脚步:

  1. [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] 但是要创建一个新的,请选择[New]→[Java Application]

  2. Create a name for run configuration and browse or type an main launching class. 为运行配置创建一个名称,然后浏览或键入一个主启动类。 In my case its the com.stackoverflow.test.Main class 就我而言,它是com.stackoverflow.test.Main

    在此处输入图片说明

  3. Now when you export as shown in the above example, you select the run configuration 现在,如上例所示导出时,选择运行配置

    在此处输入图片说明

  4. Run the jar like above. 像上面一样运行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: 提取的罐子:

在此处输入图片说明

I found that when you changed: 我发现当您更改时:

Package required libraries into generated jar 将所需的库打包到生成的jar中

to

Extract required libraries into generated jar 将所需的库提取到生成的jar中

in the Set up Launch config it worked for me. 在“设置启动”配置中对我有用。

提取所需的库按钮

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中创建可运行的Jar(或其他IDE或手动创建):”( https://www.cefns.nau.edu/~edo/Classes /CS477_WWW/Docs/RunnableJarsinEclipse.html ),它提到我们需要做四件事:

  1. Make sure create a package for our code, not just create a project in Eclipse 确保为我们的代码创建一个包,而不仅仅是在Eclipse中创建一个项目
  2. 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) 在代码的主包下为资源文件创建子包(子文件夹)(请注意,子包位于主包下,不仅在项目中)
  3. get all file references from getResource() (getting the URL reference) 从getResource()获取所有文件引用(获取URL引用)
  4. Export of files as a runnable JAR in Eclipse (File -> Export... -> select Runnable JAR files -> next -> ...) 在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图像,并且互联网上存在许多有关如何从URL获取SWT图像或如何将ImageIcon转换为SWT图像的问题,以下是用于从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();
}

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));

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. WE4是我的类构造函数名称。 Hope it helps. 希望能帮助到你。

The problem was I had this project in my Windows profile... that had an "!" 问题是我的Windows配置文件中有这个项目……它带有“!” in it... (DeNitE! -> was the name of my Windows profile) 在其中...(DeNitE!->是我的Windows配置文件的名称)

As soon as I changed it to DeNitE (without the !) it worked fine... 我将其更改为DeNitE(不带!)后,它就可以正常工作...

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 has to be an Image/BufferedImage. 标头必须是Image / BufferedImage。 This goes to the folder that the runnable jar is within and looks for a folder called resources. 这将转到可运行jar所在的文件夹,并寻找一个名为resources的文件夹。 http://i.stack.imgur.com/x8xtO.png http://i.stack.imgur.com/x8xtO.png

Another work around is if you put your resource file, an your .jar file in the same location, it should work. 另一个解决方法是,如果将资源文件(.jar文件)放在相同的位置,则它应该可以工作。 The only drawback is you have to have the resources with the jar file at all times. 唯一的缺点是您必须始终将资源与jar文件一起使用。

If that's not an issue you can do it this way. 如果那不是问题,您可以通过这种方式进行。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM