Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java 9.0 | ClassLoader::getResourceAsStream: NullPointerException

This piece of code down below, where I take my file from folder which is inside the "/resource" folder, works fine for me in Java 8:

//e.g fileName = "folder0/file1.extension2"

ClassLoader classLoader = ResourceLoader.class.getClassLoader();
InputStream in = classLoader.getResourceAsStream(fileName);
Scanner scanner = new Scanner(in, "UTF-8");

In Java 9 it does not, classLoader.getResourceAsStream(fileName) returns null:

java.lang.NullPointerException: source

However, if I use files straight from "/resource" folder, this works fine:

fileName = "file0.extension1"; // It works!

My question is quite obvious, to be honest, there are two of them:

  1. What is going on?
  2. How can fix that?

Here is my project structure:

enter image description here

*.jar output structure:

*.jar:
- javaFolder1
    -javaFolder1.1
        -ResourceLoader.class
        -jclass1.1.2.class
        -jclass1.1.3.class
    -javaFolder1.2
- javaFolder2
    - ..
- ..

- unreachableResourceFolderImTryingToAccess1
    -resource1.1.ext
    -resource1.2.ext
- unreachableResourceFolderImTryingToAccess2
    - ..
- unreachableResourceFolderImTryingToAccess3
    - ..
-resource0.1.ext
-resource0.2.ext
- ..

- somedll1.dll
- somedll2.dll
- ..
like image 206
voismager Avatar asked Dec 02 '17 23:12

voismager


People also ask

What is getResourceAsStream Java?

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

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

In the module_info.java file, you have to open the files, for example, the package name, in which you have the files is "resources" and, if that package is inside another called "resources_module" the code will be:

exports resources_module;
opens resources;
opens resources.(the name of another folder inside the package);

And now you can access to that folder.

HOW TO ACCESS TO RESOURCES FROM ANOTHER CLASS

  1. Inside the package "resources_module" create a empty class
  2. From another class (including other modules), to get an InputStream you have to call the class you created before (the empty class) like this:

    (Your empty class).class.getResourceAsStream(path);

like image 160
oxixes Avatar answered Oct 07 '22 16:10

oxixes