从jar文件加载图像

我有一个完全可以从Netbeans IDE运行的应用程序,但是当从dist目录中的jar文件运行时,不会加载必要的图像。

我花了一个半天阅读这个和其他论坛,试图找到答案,但我无法让jar图像工作。

在这里,他是我的代码摘录:

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

我尝试了很多相对路径的组合,包括“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 

等等…

我知道我遗漏了一些基本的东西,但我不确定是什么。 我怀疑它与清单文件或类路径有关。

希望有人可以指出我正确的方向……

编辑:问题似乎是这样

 URL url = Terrain.class.getResource("/fantasyhexwar/Images/" + str); 

返回null。 图像肯定在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"); 

但直接从JAR执行时它都不起作用……

当我尝试从命令行运行时,我得到:

java -cp .; FantasyHexWar.jar FantasyHexWarApp

 Exception in thread "main" java.lang.NoClassDefFoundError: FantasyHexWarApp Caused by: java.lang.ClassNotFoundException: FantasyHexWarApp at java.net.URLClassLoader$1.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. 

我的文件名有点粗心。 例如,一个文件名为“save map.png”,但应用程序正在寻找“Save Map.png”。

从驱动器加载文件时这很好用,但是当转换成URL并直接从jar加载时,它就完全不同了。

因此,jar中的资源文件名似乎区分大小写

阅读Swing教程中有关如何使用图标的部分, 以获取有关其工作原理的详细说明。

我有同样的问题。 解决方案让我看到有关getToolkit()函数的意见。 在我的情况下,我正在构建一个桌面应用程序,函数getToolkit()对我来说是不可用的。 相反,我需要使用此代码:

 Toolkit.getDefaultToolkit().getImage(ClassLoader.getSystemResource(path)); 

该程序现在可以在Netbeans环境中运行。

可能值得一试。

Image theImage = getToolkit().getImage(getClass().getResource("/path/to/image"))
Image theImage = getToolkit().getImage(getClass().getResource("/path/to/image")) 

我不认为它与从jar加载图像有任何关系。 你得到的例外说: –

 java.lang.ClassLoader.loadClass(Unknown Source) Could not find the main class: FantasyHexWarApp. Program will exit. 

这意味着,jar中没有FantasyHexWarApp主类。 修复它,它应该工作得很好。 🙂

使用此选项可显示.jar文件中的图像

 ClassLoader.getSystemResource("images/Springz3.png"); 

如果图像在jar文件中,请在fantasyhexwar / Images /。*下,

 Image image = getToolkit().getImage(ClassLoader.getSystemResource("fantasyhexwar/Images/plains100.gif")); 

将工作。