java 从 jar 文件加载图像
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4971587/
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
Load images from a jar file
提问by Fysh
I have an application that works perfectly from the Netbeans IDE, but when run from the jar file in the dist directory does not load the necessary images.
我有一个在 Netbeans IDE 中完美运行的应用程序,但是当从 dist 目录中的 jar 文件运行时,不会加载必要的图像。
I have spent 1 1/2 days reading this and other forums, trying to find an answer, but I can't get the jar images to work.
我花了 1 1/2 天的时间阅读这个论坛和其他论坛,试图找到答案,但我无法让 jar 图像工作。
Here he is an extract from my code:
这是他从我的代码中摘录的:
String str = t.getText() + "100.gif";
Image img = null;
if (t != HIDDEN)
{
ClassLoader cldr = Terrain.class.getClassLoader();
URL url = cldr.getResource("fantasyhexwar/Images/" + str);
if (url == null)
JOptionPane.showMessageDialog(null, "no good");
img = ImageIO.read(url);
t.setImage(img);
}
I have tried many combinations of relative path, including "images/", "/images/", etc. The images are in the jar file:
我尝试了很多相对路径的组合,包括“images/”、“/images/”等,图片在jar文件中:
fantasyhexwar/Images/plains100.gif
fantasyhexwar/Images/quarry60.gif
fantasyhexwar/Images/ram80.gif
fantasyhexwar/Images/save map40.gif
fantasyhexwar/Images/scout80.gif
fantasyhexwar/Images/settler80.gif
fantasyhexwar/Images/ship80.gif
etc...
等等...
I know I am missing something fundamental, but I'm not sure what. My suspicion is that it is something to do with the manifest file or possibly class path.
我知道我错过了一些基本的东西,但我不确定是什么。我怀疑这与清单文件或可能的类路径有关。
Hopefully someone can point me in the right direction...
希望有人能指出我正确的方向......
EDIT: The problem seems to be that
编辑:问题似乎是
URL url = Terrain.class.getResource("/fantasyhexwar/Images/" + str);
returns null. The images are definitely in the JAR, and in desperation I have also tried all possible relative paths, with code like this:
返回空值。图像肯定在 JAR 中,无奈之下我也尝试了所有可能的相对路径,代码如下:
ClassLoader cldr = Terrain.class.getClassLoader();
URL url = Terrain.class.getResource("/fantasyhexwar/Images/" + str);
if (url == null)
url = cldr.getResource("/fantasyhexwar/fantasyhexwar/Images/" + str);
if (url == null)
url = cldr.getResource("fantasyhexwar/fantasyhexwar/Images/" + str);
if (url == null)
url = cldr.getResource("/fantasyhexwar/Images/" + str);
if (url == null)
url = cldr.getResource("/Images/" + str);
if (url == null)
url = cldr.getResource("Images/" + str);
if (url == null)
url = cldr.getResource("/" + str);
if (url == null)
url = cldr.getResource(str);
if (url == null)
JOptionPane.showMessageDialog(null, "no good");
But none of it works when executing directly from the JAR...
但是当直接从 JAR 执行时,它都不起作用......
When I try to run from the command line, I get:
当我尝试从命令行运行时,我得到:
java -cp .;FantasyHexWar.jar FantasyHexWarApp
java -cp .;FantasyHexWar.jar FantasyHexWarApp
Exception in thread "main" java.lang.NoClassDefFoundError: FantasyHexWarApp
Caused by: java.lang.ClassNotFoundException: FantasyHexWarApp
at java.net.URLClassLoader.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
Could not find the main class: FantasyHexWarApp. Program will exit.
回答by Fysh
I was a little careless with my filenames. For example one file was called "save map.png", but the application was looking for "Save Map.png".
我对我的文件名有点粗心。例如,一个文件名为“save map.png”,但应用程序正在寻找“Save Map.png”。
This worked fine when loading files from the drive, but when turned into a URL and loaded directly from the jar, it made all the difference.
这在从驱动器加载文件时工作正常,但是当转换为 URL 并直接从 jar 加载时,它就完全不同了。
Therefore, it seems that resource file names in a jar are case-sensitive.
因此,jar 中的资源文件名似乎是区分大小写的。
回答by camickr
Read the section from the Swing tutorial on How to Use Iconsfor a detailed explanation of how this works.
阅读 Swing 教程中关于如何使用图标的部分,详细了解其工作原理。
回答by user2767882
If the images are in the jar file, under fantasyhexwar/Images/.*,
如果图像在 jar 文件中,在Fantasyhexwar/Images/.* 下,
Image image = getToolkit().getImage(ClassLoader.getSystemResource("fantasyhexwar/Images/plains100.gif"));
will work.
将工作。
回答by Darkcavie
I had the same problem. The solution came to me reading the opinions about getToolkit() function. In my case I am building a desktop application, and the function getToolkit() is not avalaible for me. Instead I need to use this code:
我有同样的问题。解决方案是我阅读有关 getToolkit() 函数的意见。就我而言,我正在构建一个桌面应用程序,而函数 getToolkit() 对我来说不可用。相反,我需要使用此代码:
Toolkit.getDefaultToolkit().getImage(ClassLoader.getSystemResource(path));
The program now functions in and out the Netbeans Environment.
该程序现在在 Netbeans 环境内外运行。
回答by Basil
Use this to display images from .jar files
使用它来显示 .jar 文件中的图像
ClassLoader.getSystemResource("images/Springz3.png");
回答by limc
I don't think it has anything to do with loading images from jar. The exception you get says:-
我认为这与从 jar 加载图像没有任何关系。你得到的例外说:-
java.lang.ClassLoader.loadClass(Unknown Source) Could not find the main class: FantasyHexWarApp. Program will exit.
That means, FantasyHexWarApp
main class is missing from the jar. Fix that and it should work just fine. :)
这意味着,FantasyHexWarApp
jar 中缺少主类。解决这个问题,它应该可以正常工作。:)
回答by user489041
Might be worth giving this a shot.
可能值得一试。
Image theImage = getToolkit().getImage(getClass().getResource("/path/to/image"))