Listing All Observable Modules
The java command with the –list-modules option (no short form) lists the system modules that are installed in the JDK, and then exits. These modules are available to every application. This lengthy list gives an idea of how the JDK has been modularized. The java command lists each module with its version; in this case, it is Java 17.0.2. Module names starting with the prefix java implement the Java SE Language Specification, whereas those starting with jdk are JDK specific. The reader will no doubt recognize some names, specially java.base.
The system modules are found in the jmods directory under the installation directory of the JDK. These are JMOD files having a module name with the extension “.jmod”. JMOD files have a special non-executable format that allows native binary libraries and other configuration files to be packaged with bytecode artifacts that can then be linked to create runtime images with the jlink tool (p. 1222).
>java –list-modules
[email protected]
…
[email protected]
…
[email protected]
…
[email protected]
…
Specifying a module path in the java command below not only lists the system modules, but also the modular JARs found in the specified module path—in other words, all observable modules. In the java command below, the absolute path of all JARs found in the mlib directory is listed last in the output.
>java –module-path mlib –list-modules
[email protected]
…
[email protected]
…
[email protected]
…
controller file:…/adviceApp/mlib/controller.jar
main file:…/adviceApp/mlib/main.jar
model file:…/adviceApp/mlib/model.jar
view file:…/adviceApp/mlib/view.jar
Describing the Module Descriptor of a JAR
Both the java tool and the jar tool have the –describe-module option (short form: -d) to show the information contained in the module descriptor of a JAR. Both commands are used respectively below.
>java –module-path mlib –describe-module main
main file:…/adviceApp/mlib/main.jar
requires java.base mandated
requires controller
contains com.passion.main
>jar –file mlib/main.jar –describe-module
main jar:file:…/adviceApp/mlib/main.jar/!module-info.class
requires controller
requires java.base mandated
contains com.passion.main
main-class com.passion.main.Main
Note that in the java command, the –describe-module option requires a module name, whereas that is not the case in the jar command, where the –file option specifies the modular JAR.
Since the module descriptor is a Java bytecode class file, it must be disassembled to display its information. In both cases, first the module name is printed, followed by the path of the JAR. In the case of the jar command, the name module-info.class is appended to the path of the JAR. In both cases, the names of modules required (java.base and controller) are reported. The main module also contains an internal package (com.passion.main). Not surprisingly, the java.base module is mandated. However, only the jar command reports that the main-class is com.passion.main.Main. The jar command is useful to find the entry point of an application.
The –describe-module option (short form: -d) can also be used on a system module. The following java command describes the module descriptor of the java.base system module. The command lists all modules the java.base module exports, uses, exports qualified, and contains (p. 1177). As can be expected from its status as a mandated module for all other modules, it does not require any module.
>java –describe-module java.base
[email protected]
exports java.io
exports java.lang
…
uses java.util.spi.CurrencyNameProvider
uses java.util.spi.TimeZoneNameProvider
…
qualified exports jdk.internal to jdk.jfr
qualified exports sun.net.www to java.desktop java.net.http jdk.jartool
…
contains com.sun.crypto.provider
contains com.sun.java.util.jar.pack
…