从jar文件加载图像 [英] Load images from a jar file

查看:107
本文介绍了从jar文件加载图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.

我有花了1 1/2天阅读这个和其他论坛,试图找到答案,但我无法让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.

这里他是一个提取物从我的代码:

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

我尝试了很多相对路径组合,包括images /, / images /等图像在jar文件中:

I have tried many combinations of relative path, including "images/", "/images/", etc. The images are in the jar file:

 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

等......

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

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...

编辑:
问题似乎是

The problem seems to be that

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

返回null。这些图像肯定在JAR中,在绝望中我也尝试了所有可能的相对路径,代码如下:

returns null. The images are definitely in the JAR, and in desperation I have also tried all possible relative paths, with code like this:

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执行时它都不起作用...

But none of it works when executing directly from the 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$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。

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".

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

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.

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

Therefore, it seems that resource file names in a jar are case-sensitive.

这篇关于从jar文件加载图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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