How to list non-system modules in Java 9

In the Java 9 modular system, you can find system modules using

Set<ModuleReference> ms = ModuleFinder.ofSystem().findAll();

These objects ModuleReferencecan 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 get ModuleReferencefrom this Module(so I can’t list the contents of the module except the packages), and not the system Moduleone 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#ucptype field is URLClassPathnot displayed and locked in JRE 9, so you cannot get it by introspection.)
+4
1

:

Set<String> systemModuleNames = ModuleFinder
        .ofSystem()
        .findAll()
        .stream()
        .map(moduleRef -> moduleRef.descriptor().name())
        .collect(Collectors.toSet());

List<Module> nonSystemModules = ModuleLayer
        .boot()
        .modules()
        .stream()
        .filter(m -> !systemModuleNames.contains(m.getName()))
        .collect(Collectors.toList());

System.out.println(nonSystemModules);

, nonSystemModules, , , (, ).

+4

Source: https://habr.com/ru/post/1691215/


All Articles