I have the following situation:
- JAR A has JAR B as dependency
- JAR B is packed with some resources that are loaded when JAR A calls specific methods of JAR B (loaded once and for all the lifecycle of JAR B calls)
- I am using Java SE 11 with IntelliJ 2021.1.3
JAR B resources tree is something like the following:
- resources
- data
- file.txt
- tariffs
- folder1
- file.xslx
Resources are loaded through the following method:
private InputStream getPath(String nomeFile) {
ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
return classLoader.getResourceAsStream(DATA_FOLDER_NAME.concat(File.separator).concat(nomeFile));
}
And then managed through a BufferedReader.
Everything works fine when running mvn test (or application tests) withing JAR B project or when consuming JAR B from JAR A in a Unix environment.
When consuming JAR B from JAR A in a Windows 10 environment the getPath method returns a null InpuStream object thus a NullPointerException from the BufferedReader:
java.lang.NullPointerException: null
at java.base/java.io.Reader.<init>(Reader.java:167)
at java.base/java.io.InputStreamReader.<init>(InputStreamReader.java:72)
...
I tried to change the File.separator to hardcoded "/" in the method and seems like everything works also on Windows, but is failing in other places (where resources are managed) since I suppose Paths need to be hand-fixed.
I tried to change the loader to: this.getClass().getResourcesAsStream(...) and other workaround with no luck.
My question is: is there a way to make the program work as expected also on Windows without changing the above code? Are there any settings I am missing?
Thank you, Alberto