How to get a list of all modules programmatically in Java 9?

How can I get a list of all the modules in the current JVM instance through Java code? Is it possible? If so, how?

+8
java java-9 java-module
source share
2 answers
ModuleLayer.boot().modules().stream() .map(Module::getName) .forEach(System.out::println); 
+10
source share

Using a jar or module catalog as input for your application, you can use ModuleFinder to start further by using findAll to find the set of all module references that this crawler can find.

 Path dir1, dir2, dir3; ModuleFinder finder = ModuleFinder.of(dir1, dir2, dir3); Set<ModuleReference> moduleReferences = finder.findAll(); 

It is easily possible with a command line parameter to display modules as:

 java -p <jarfile> --list-modules 

which should be sufficient, though, if you do not want to intentionally go into configuring things with ModuleLayer and Configuration java.lang.module , as indicated in @Alan's answer .

+3
source share

All Articles