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:
- What is going on?
- How can fix that?
Here is my project structure:
*.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
- ..
resource
folder end up on your classpath? – ViveJDK 9.0.1
vsJDK 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. – Assets/folder0/file1.extension2
– Meadow