getResourceAsStream()在jar中返回null但在eclipse中很好 [英] getResourceAsStream() returning null in jar but fine in eclipse

查看:163
本文介绍了getResourceAsStream()在jar中返回null但在eclipse中很好的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个使用LWJGL的Java程序。它在Eclipse中运行良好,但是当我尝试将其编译为jar文件时,它会崩溃,给我一个NullPointerException。之前有人问过,我知道,但我似乎没有得到有效的答案。有什么帮助吗?提前致谢!

I have a program in Java using LWJGL. It runs fine in Eclipse, but when I try to compile it as a jar file it crashes, giving me a NullPointerException. It's been asked before, I know, but I don't seem to get an answer that works. Any help here? Thanks in advance!

似乎遇到问题的是TextureHelper类:

The thing that seems to be having the problem is the class TextureHelper:

public class TextureHelper {

    public static Texture LoadTexture(String texture)
    {
        try {
            return TextureLoader.getTexture("PNG", ResPlaceholder.class.getResourceAsStream(texture));
        } catch (IOException e) {
            e.printStackTrace();
            return null;
        }
    }

}

一些注意事项:

我也尝试了/ res /+纹理,以及许多类似的东西。

I've also tried "/res/" + texture, as well as many other things like it.

ResPlaceholder是一个位于我存储所有图像的res文件夹中的类。这是一个空白的空类。

ResPlaceholder is a class that sits in the res folder where I store all my images. It's a blank empty class.

这在Eclipse中完美运行。

This works perfectly in Eclipse.

我的jar有这些文件夹(就像例如):

My jar has these folders (just as an example):

foo.jar

---- core

----core

-------- TextureLoader

--------TextureLoader

---- res

--------什锦图像文件

-------- Assorted Image Files

-------- ResPlaceholder

-------- ResPlaceholder

这与eclipse中的软件包相同。

This is the same as the packages in eclipse.

你会得到任何帮助,我会被困在这几天没有进展。

Any help you have would be appreciated, I've been stuck on this for days with no progress.

编辑:

META-INF / MANIFEST.MF

META-INF/MANIFEST.MF

Main.class config /

Main.class config/

config / Images.class

config/Images.class

core /

core / LevelLoader.class

core/LevelLoader.class

core / TextureHelper.class

core/TextureHelper.class

core / TileSet.class

core/TileSet.class

- 解除其他一些与此无关的东西 -

-Skipping some other stuff that has nothing to do with this-

res /

res / ResPlaceholder.class

res/ResPlaceholder.class

res / BlankImg.png

res/BlankImg.png

res / test.txt

res/test.txt

res / testImg.png

res/testImg.png

res / testTiles.png

res/testTiles.png

推荐答案

如果路径字符串类似于: / res /testImg.png 它应该有效。

If the path String is something like: /res/testImg.png it should work.

ie,

String resourcePath = "/res/testImg.png";
InputStream inStream = ResPlaceholder.class.getResourceAsStream(resourcePath );
BufferedImage img = ImageIO.read(inStream);

// use the img BufferedImage here
// for example:
ImageIcon icon = new ImageIcon(img);
JOptionPane.showMessageDialog(null, icon);

顺便说一句,在小型测试程序中测试问题并检查新概念总是好的。在你的情况下,我将创建一个简单的小程序,只有一个主方法,试图提取jar的图像资源并将其显示在选项窗格中,就像我上面的小代码一样。我会尽量避免长线,而是将每一步分开在自己的线上,再次类似于我上面的帖子。这样,如果抛出异常,该行上的代码将更多地提供有关导致错误的信息。

As an aside, it's always good to test problems and check new concepts in small test programs. In your case, I would create a small simple program with just a main method that attempts to extract a jar's image resource and display it in an option pane much like my small code above does. I'd try to avoid long lines and instead separate each step on its own line, again similar to my post above. This way if an exception is thrown, the code on the line will be much more informative as to what is causing the error.

这篇关于getResourceAsStream()在jar中返回null但在eclipse中很好的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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