English 中文(简体)
从jar文件加载图像
原标题:Load images from a jar file
  • 时间:2011-02-11 16:35:43
  •  标签:
  • java

我有一个在Netbeans IDE中运行良好的应用程序,但当从dist目录中的jar文件运行时,不会加载必要的映像。

我花了1天半的时间阅读这个和其他论坛,试图找到答案,但我无法让罐子的图像发挥作用。

以下是我的代码摘录:

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

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

希望有人能给我指明正确的方向。。。

EDIT: The problem seems to be that

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幻想HexWarApp

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.
最佳回答

我对文件名有点粗心。例如,一个文件名为“savemap.png”,但应用程序正在查找“SaveMap.png”。

当从驱动器加载文件时,这一点很好,但当转换为URL并直接从jar加载时,一切都不同了。

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

问题回答

如果图像在jar文件中,在fantasyhexwar/images/.*下,

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

将起作用。

阅读Swing教程中关于如何使用图标获取有关如何使用图标的详细说明。

我也有同样的问题。在阅读关于getToolkit()函数的意见时,我想到了这个解决方案。在我的情况下,我正在构建一个桌面应用程序,而函数getToolkit()对我来说是不可用的。相反,我需要使用以下代码:

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

该程序现在在Netbeans环境内外运行。

也许值得一试。


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




相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签