11

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
- ..
voismager
  • 433
  • 4
  • 19
  • How does the `resource` folder end up on your classpath? – Oliver Charlesworth Dec 02 '17 at 23:25
  • @OliverCharlesworth, all files and folders in **/src/main/resources** are onto the root of my .jar file. – voismager Dec 02 '17 at 23:38
  • Didn't got the same behavior while exporting the runnable jar with Maven and `JDK 9.0.1` vs `JDK 1.8.0_121` , hmm although strange but got the same behavior while exporting from eclipse, noticed that eclipse creates a folder *resources* inside the jar file. – AntJavaDev Dec 03 '17 at 00:08
  • Could you share the resource directory structure that you're using and you end up with after creating a JAR. – Naman Dec 03 '17 at 00:43
  • If your ResourceLoader is on the class path then the resource you are looking to locate it also on the class path, then calling ResourceLoader's defining loader's getResourceAsStream should behave the same in JDK 9 as it did in JDK 8. If you can create a small example to demonstrate what you are seeing then it would be very useful. – Alan Bateman Dec 03 '17 at 08:43
  • @nullpointer I've added the screenshot, if that's what you mean. – voismager Dec 03 '17 at 11:12
  • 1
    try `/folder0/file1.extension2` – Antoniossss Dec 03 '17 at 11:14
  • @Antoniossss it doesn't work either. – voismager Dec 03 '17 at 12:44
  • 2
    If the JAR file really contains the resources listed in the updated question then calling ResourceLoader.class.getResourceAsStream(name), where name is any of "/resource0.1.txt", "/unreachableResourceFolderImTryingToAccess1/resource1.1.ext", "jclass1.1.2.class", "/javaFolder1/javaFolder1.1/jclass1.1.2.class" should work. Ditto if ResourceLoader.class.getClassLoader().getResourceAsStream(name) is called with "resource0.1.txt", "unreachableResourceFolderImTryingToAccess1/resource1.1.ext", "/javaFolder1/javaFolder1.1/jclass1.1.2.class". No difference between JDK 8 and JDK 9. – Alan Bateman Dec 03 '17 at 13:28
  • I'm struggling with this too, but it seems non-class resources are not accessibly, at least thats how I understand this post from Mark Reinhold: https://stackoverflow.com/a/45173837/1961102 (only when the package was opened by the jdk9-module) – FibreFoX Mar 25 '18 at 22:09

1 Answers1

5

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

oxixes
  • 101
  • 1
  • 10