Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

getResourceAsStream() returns null only when in jar file

Tags:

java

jar

netbeans

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

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.

The jar file contents are as follow: http://i.stack.imgur.com/1y8i8.png

While the src folder in the netbeans project folder is the following: http://i.stack.imgur.com/AXj7s.png

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!

like image 717
Queek Avatar asked May 28 '15 19:05

Queek


People also ask

What does getResourceAsStream return?

The getResourceAsStream method returns an InputStream for the specified resource or null if it does not find the resource. The getResource method finds a resource with the specified name. It returns a URL to the resource or null if it does not find the resource.

Should we close getResourceAsStream?

You should always close streams (and any other Closeable, actually), no matter how they were given to you. Note that since Java 7, the preferred method to handle closing any resource is definitely the try-with-resources construct.

How does getResource work in Java?

The getResource() method of java Class class is used to return the resources of the module in which this class exists. The value returned from this function exists in the form of the object of the URL class.

How do you send paths in getResourceAsStream?

getResourceAsStream , send the absolute path from package root, but omitting the first / . If you use Class. getResourceAsStream , send either a path relative the the current Class object (and the method will take the package into account), or send the absolute path from package root, starting with a / .


1 Answers

I think the getResourceAsStream is not reading from filesystem, the leading slash will try to read from the root of the classpath (if you add a directory will read from the root). If you don't put the leading slash, you will read from the package the class is from.

Did you add the directory/subdirectories you want to read the file from in the classpath?

like image 172
Luca Foppiano Avatar answered Oct 04 '22 11:10

Luca Foppiano