getResourceAsStream()仅在jar文件中返回null [英] getResourceAsStream() returns null only when in jar file

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

问题描述

我知道很多次都会问过同样的问题,但是经过所有已经发布的答案我找不到解决方案。

I know the same question has been asked many many times, but going through all the answers already posted I couldn't find a solution.

首先这里是我在我的项目中遇到它后用来测试问题的代码:

First of all here's the code I'm using to test the issue after i encountered it in my project :

InputStream test;

System.out.println(this.getClass());
System.out.println(("classpath is: " + System.getProperty("java.class.path")));

test = getClass().getResourceAsStream("/pairs/images/100/back/back1.png");

if (test == null)
{
    System.out.println("getClass().getResourceAsStream(\"/pairs/images/100/back/back1.png\") is null");
    test = GridPanel.class.getClassLoader().getResourceAsStream("pairs/images/100/back/back1.png");
}

if (test == null)
{
    System.out.println("GridPanel.class.getClassLoader().getResourceAsStream(\"pairs/images/100/back/back1.png\") is null");
    test = ClassLoader.getSystemClassLoader().getResourceAsStream("pairs/images/100/back/back1.png");
}

if (test == null)
{
    System.out.println("ClassLoader.getSystemClassLoader().getResourceAsStream(\"pairs/images/100/back/back1.png\") is null");
    Thread.currentThread().getContextClassLoader().getResourceAsStream("pairs/images/100/back/back1.png");
}

if (test == null)
    System.out.println("Thread.currentThread().getContextClassLoader().getResourceAsStream(\"pairs/images/100/back/back1.png\") is null");

因此,根据标题,对getResourceAsStream()的这些调用的每个人都返回null,但仅在执行jar时文件,当在IDE(Netbeans 8.0.2)中启动时,它们都返回正确的流(单独测试不使用这段代码),我可以使用它。

So as per title everyone of these calls to getResourceAsStream() returns null, but only when executin the jar file, when launching inside the IDE (Netbeans 8.0.2) they all return the correct stream (tested individually not with this piece of code) and I can work with it.

jar文件内容如下:

The jar file contents are as follow:

netbeans项目文件夹中的src文件夹如下:

While the src folder in the netbeans project folder is the following:

所以我真的很困惑那一刻,我在使用netbeans构建时尝试使用默认设置,但我对这个问题感到失望。

So I'm really baffled at the moment, I've tried using the default settings when building with netbeans but I'm losing my head around this issue.

任何提示或想法都会非常感激!

Any hints or ideas would be much appreciated!

推荐答案

TL; DR这是由于NTFS权限的一些愚蠢问题,移动jar使我工作

TL;DR It was due some stupid issue with NTFS permission, moving the jar made it work

所以我现在觉得非常愚蠢......

So I feel very stupid right now...

我试过在Linux环境下运行它一切都按照假设运作。
这些调用中的每一个都返回一个指向资源的非null InputStream。

I tried running it in a Linux environment and everything worked as supposed. Each one of those calls was returning a non-null InputStream pointing to the resource.

所以我做了一些非常简单的事情:只是在已编译的jar中移动Windows检查它是否与NTFS权限有关(请记住,我使用的是管理员帐户,我可以在这些文件夹中自由编写,读取和执行)。
将它移动到项目的根目录不起作用,但是将它移到桌面上工作。

So I did something very simple: just moved around the compiled jar in Windows to check if it had something to do with NTFS permissions (keep in mind that I'm using an administrator account and I can freely write, read and execute in those folders). Moving it to the root of the project didn't work, but moving it on the desktop WORKED.

令我惊讶的是,在其他一些部分我正在做的项目

What amazes me more is that in some other parts of the projects I'm doing

URL jarUrl = PairsDeck.class.getProtectionDomain().getCodeSource().getLocation();

读取jar文件的全部内容,即使在旧目录中也是如此,所以这就是为什么我从一开始就没有想过搞过权限(我从具有管理员权限的命令提示符启动它)。

to read the whole content of the jar file and that works every time even in the old directory, so that's why I hadn't thought of screwed permissions since the beginning (and I was launching it from a command prompt with admin privileges).

所以我想这个程序可以由于NTFS权限的一些问题,我在netbeans项目文件夹中读取了它的jar,但没有从中加载资源。

So I guess somehow the program can read its jar with that call but not load resources from it inside my netbeans project folder due to some issues with NTFS permissions.

添加一些混淆

File file = new File(jarUrl.toURI());
file.canWrite();
file.canRead();
file.canExecute(); 

所有上述回报都是真的。

all of the above return true.

我只是想知道是否有办法更好地理解这个问题,并且可能有办法从java代码中发出一些权限错误。

I'm just curious to know if there was a way to understand better the issue and maybe have a way to signal some permissions error from the java code.

全心全意地感谢给所有参与帮助的人!

Thanks wholeheartedly to everybody involved for the help!

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

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