Update: I answered my own question here:
Scanning classpath/modulepath in runtime in Java 9
--
[Old question -- obsolete:]
In the Java 9 module system, you can find system modules using
Set<ModuleReference> ms = ModuleFinder.ofSystem().findAll();
These ModuleReference
objects can then be used to list the contents of each module, using:
for (ModuleReference m : ms) {
System.out.println(m.descriptor().name());
System.out.println(" " + m.descriptor().toNameAndVersion());
System.out.println(" " + m.descriptor().packages());
System.out.println(" " + m.descriptor().exports());
Optional<URI> location = m.location();
if (location.isPresent()) {
System.out.println(" " + location.get()); // e.g. "jrt:/java.base"
}
m.open().list().forEach(s -> System.out.println(" " + s));
}
And to get the module of the current class, you can use
Module m = getClass().getModule();
but I can't seem to get a ModuleReference
from this Module
(so I can't list the contents of the module, other than the packages), and the non-system Module
s are not listed by ModuleFinder
.
Two questions:
- How do I enumerate the resources in non-system modules?
- How are non-module classpaths read in JRE 9? Am I just supposed to rely on the
java.class.path
system property? (TheAppClassLoader#ucp
field of typeURLClassPath
is not visible, and is locked down in JRE 9, so you can't get it by introspection either.)