So far until non-modularized java, you would simply put a file in src/main/java/resources
make sure it is in classpath and then load it with
file = getClass().getClassLoader().getResourceAsStream("myfilename");
from pretty much anywhere in the classpath.
Now with modules, the plot thickens.
My project setup is the following:
module playground.api {
requires java.base;
requires java.logging;
requires framework.core;
}
Config file is placed inside src/main/resources/config.yml
.
Project is run with
java -p target/classes:target/dependency -m framework.core/com.framework.Main
Since the main class does not reside in my own project, but in an external framework module, it can't see config.yml
. Now the question is, is there a way to somehow put my config file into the module or open it up? Do I have to change the way file is loaded by framework upstream?
I tried using "exports" or "opens" in module-info but it wants to have a package name, not a folder name.
How to achieve this in best practical way so it would work as in Java 8 and with as little changes as possible?
com.framework.Main
read resources usingClass.getResource
?-cp
. Can you instead add--add-moduels playground.api
to the command line. The initial module (the module you specify to -m) is com.framework.Main and I assume that nobody requires playground.api so it's not being resolved (you can quickly check this by adding--show-module-resolution
to trace resolution at startup).