How can I get list of all modules in current JVM instance via Java code? Is it possible? If yes, then how?
How to get list of all modules programmatically in Java 9?
6.2k Views Asked by Pavel_K At
2
There are 2 best solutions below
0

Using a jar or directory of modules as an input for your application, you can possibly use a ModuleFinder
to start off with and further making use of the findAll
to find the set of all module references that this finder can locate.
Path dir1, dir2, dir3;
ModuleFinder finder = ModuleFinder.of(dir1, dir2, dir3);
Set<ModuleReference> moduleReferences = finder.findAll();
This is easily possible with the command line option to list the modules as:
java -p <jarfile> --list-modules
which should be sufficient though, if you do not want to intentionally get into tweaking things with the ModuleLayer
and the Configuration
of the java.lang.module
as pointed out precisely by @Alan's answer.