无法在导出的jar中找到/打开图像 [英] Unable to locate/open image inside exported jar

查看:222
本文介绍了无法在导出的jar中找到/打开图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经探讨了Stack Overflow中与此问题相关的每一个解决方案,我遇到的解决方案仅在我的IDE(Eclipse Mars)中工作,但是当导出时它们都失败。



我有一个图像文件,我只想从我的导出的jar里面加载。





从项目布局中可以看到,图像是直接在项目文件夹 CooldownTrackerJava 。要参考它,我已经尝试了各种方法。

  //我的类的名称是AddItemDialog 

//方法1
String filePath = File.separator +Images+ File.separator +questionMark.png;
InputStream inputStream = AddItemDialog.class.getResourceAsStream(filePath);
BufferedImage img = ImageIO.read(inputStream);

我以几种方式尝试过方法1。我在文件路径的开头尝试了没有 File.separator 。我明白前者是指一个绝对的路径,而后者是相对的。我也尝试将Images文件夹移动到我的类文件所在目录下。



当导出和测试多次时,所有这些都失败了给我一个 NullPointerException

  //方法2 
String saveLocation = File.separator +Images+ File.separator +questionMark.png;
Image img = Toolkit.getDefaultToolkit()。getImage(getClass()。getResource(saveLocation));

解决方案

解决方案是一个两部分的努力。首先,我将图像文件夹移动到 src 目录中,如下所示。对马修的赞许。





然后我删除 File.separator ,并用正斜杠 / 替换。感谢fge。


不要使用File.separator()。类路径上资源路径的分隔符始终为/。如果您使用Windows,您将总是会收到错误的结果。


我使用方法1的变体来测试它。两种方法都可以正常工作。

  String saveLocation =/Images/questionMark.png; 
InputStream inputStream = this.getClass()。getResourceAsStream(saveLocation);
try {
BufferedImage img = ImageIO.read(inputStream);
} catch(IOException e){
e.printStackTrace();
}


I have explored every single solution related to this problem on Stack Overflow, and the solutions I encountered worked only in my IDE (Eclipse Mars), but when exported they all failed.

I have an image file I simply want to load from inside my exported jar.

As you can see from the project layout, Images is a folder directly inside of the project folder CooldownTrackerJava. To refer to it, I've tried a variety of methods.

//My class' name is AddItemDialog

//Method 1
String filePath = File.separator + "Images" + File.separator + "questionMark.png";
InputStream inputStream = AddItemDialog.class.getResourceAsStream(filePath);
BufferedImage img = ImageIO.read(inputStream);

I tried method 1 in a few ways. I tried it with and without the File.separator at the start of the filePath. I understand that the former refers to an absolute path, while the latter is relative. I also tried moving the Images folder inside of the directory where my class files were.

All of these failed giving me a NullPointerException when exported and tested multiple times.

//Method 2
String saveLocation = File.separator + "Images" + File.separator + "questionMark.png";    
Image img = Toolkit.getDefaultToolkit().getImage(getClass().getResource(saveLocation));

As per this post I tried the above method however it still did not work.

I've tried many variations aside from the ones mentioned in this post but none of them have been successful when I exported the jar.

Do I need to add the Images folder to my build path? Should I move it to the src folder? Am I missing one obvious step? All suggestions would be highly appreciated. Also here is the layout of the exported jar file.

解决方案

The solution was a two-part effort. Firstly, I moved my Images folder into the src directory as shown below. Kudos to Matthew.

I then removed File.separator and replaced it with a forward slash/. Thanks to fge.

Do not use File.separator(). Separators for resource paths on the class path is always /. If you use Windows you will therefore always get the wrong result.

I used a variant of method 1 to test it. Both methods will work though.

String saveLocation = "/Images/questionMark.png";
InputStream inputStream = this.getClass().getResourceAsStream(saveLocation);
try {
    BufferedImage img = ImageIO.read(inputStream);
} catch (IOException e) {
    e.printStackTrace();
}

这篇关于无法在导出的jar中找到/打开图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆