Before Java9, I could get the URLClassLoader
instance and fetch the URL[]
containing all the items on the classpath: jars and folders.
How can I do that in Java9? It would be super cool if I can do that using just regular API and not forcing users to add additional VM flags or who-knows-what.
For now, I am able to get the classpath for current module:
Module module = MyClass.class.getModule();
module.getClassLoader().getResource(
classNameToResourceName(MyClass.class));
This can give me something like e.g. file:/Users/.....
. I can the same for any explicit class of 3rd party module, getting something like e.g.jar:file:///Users...
.
I know there if ModuleFinder
that finds modules that are already on some path - which is the thing I want to find here.
I don't know how to get the paths for other modules/jars without needing to explicitly use a class from the module/jar (as shown above).
System.getProperty("java.class.path")
is not working as it does not give the classpath, on Java9 example I tried it returns only the jar of the IntelliJ IDEA.
The reason is quite obvious: classpath scanning. This feature is not a new for the frameworks out there. For example, a user can build a class, annotate it and he is done - the framework would locate it and register it for him/her.
EDIT: the proposed duplicate question really is the same (about the scanning), however, the answer is helpful, but it is not complete.