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
? – Septsrc/main/resources
if it's not a package? I guess I could put it in a package but that seems like an anti-pattern. That is the first part of my confusion. And second, by top level you mean which folder? I also tried putting my config file in META-INF but no dice. – Uluopens src/main/resources
, doesn't compile ofc. – Ulutarget/classes
but it does not work. It does work if I add-cp target/classes
though. But should one use -cp when using jigsaw? It feels like I shouldn't for some reason. – Ulu-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). – Selfdrive