In the Java 9 modular system, you can find system modules using
Set<ModuleReference> ms = ModuleFinder.ofSystem().findAll();
These objects ModuleReference
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());
}
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 get ModuleReference
from this Module
(so I canβt list the contents of the module except the packages), and not the system Module
one is not listed on ModuleFinder
.
Two questions:
- How to list resources in non-system modules?
- How are non-modular class paths read in JRE 9? Should I rely on a system property
java.class.path
? (The AppClassLoader#ucp
type field is URLClassPath
not displayed and locked in JRE 9, so you cannot get it by introspection.)