Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

How can I get list of all modules in current JVM instance via Java code? Is it possible? If yes, then how?

like image 327
Pavel_K Avatar asked Sep 17 '17 17:09

Pavel_K


People also ask

What is the base module of all the modules in Java 9?

java. base is known as the "mother of Java 9 modules." In the following image, you can see the modular aspects of the system and can probably make the leap to understand how a modularized JDK means that you can also modularize your own applications. This is only a few of the 98 platform modules.

Where is Java module-info?

xml files and the Maven directory project structure. The module descriptor ( module-info. java ) needs to be located in the src/main/java directory.

What is Modul Info Java?

A module descriptor is the compiled version of a module declaration that's defined in a file named module-info. java . Each module declaration begins with the keyword module , followed by a unique module name and a module body enclosed in braces, as in: A key motivation of the module system is strong encapsulation.

What are the different modules in Java?

These modules are split into four major groups: java, javafx, jdk, and Oracle. java modules are the implementation classes for the core SE Language Specification. javafx modules are the FX UI libraries. Anything needed by the JDK itself is kept in the jdk modules.


2 Answers

ModuleLayer.boot().modules().stream()
                .map(Module::getName)
                .forEach(System.out::println);
like image 76
Alan Bateman Avatar answered Sep 19 '22 15:09

Alan Bateman


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.

like image 21
Naman Avatar answered Sep 18 '22 15:09

Naman