Module Graph of the JDK
The modules in the JDK define dependencies on other modules. These dependencies can be viewed as a graph in which the modules are the nodes and the unidirectional edges between the nodes define the dependencies between the modules. A partial module graph of the JDK is shown in Figure 19.2. The complete module graph of the JDK is of course substantially larger, but Figure 19.2 will suffice to convey its modular structure. It is also a reduced graph where by redundant dependencies, like the implicit dependencies on the java.base module (p. 1167), have been omitted.
Figure 19.2 Partial Module Graph of Java SE 17 Modules
A unidirectional edge, for example, from the module java.se to the java.desktop module signifies that the former has a dependency on the latter. The (complete) module graph of the JDK allows us to read which modules any given module depends on. The structure of the module graph in Figure 19.2 highlights two important modules: java.se at the zenith and java.base at the base of the graph, showing that the former depends on each module in the Java SE platform configuration and the latter on none of them.
Module graphs visualize the modular structure of an application, providing insights into its architecture and in discovering any anomalies. Such graphs can be built using the JDK tool jdeps (p. 1214). Other JDK tools, such as javac and java, perform analysis on the module graph of the application to ensure that all module dependencies are satisfied.
The java.base Module
The java.base module implements the fundamental APIs of the Java SE platform. It is necessary for any Java program to function, and deemed necessary for every configuration of the Java platform. Not surprisingly, it is required by every module in the JDK. In fact, every application module has an implicit dependence on the java.base module. However, it is seldom necessary for a module to explicitly specify a dependence on the java.base module.
Typically when constructing a module graph, dependency on the java.base module is only shown for a module that does not depend on any other module. Otherwise, this dependency is implicit.
The java.base module contains many important packages, such as java.lang, java.util, and java.io. The java.lang package, which contains the primordial class java.lang.Object, is imported automatically by every Java compilation unit.
Even though every module depends on the java.base module, this does not mean that all packages in it are also automatically imported. This only applies to the java.lang package. To use simple names of types in the source code, the relevant packages must be explicitly imported.
Leave a Reply