Loading Module

In the Java platform, the concept of modules is integral to the organization and execution of code. Modules are a way to encapsulate packages and resources, providing a higher level of aggregation than packages alone. This page provides an overview of the different types of modules and how they are loaded in the Java environment.

Overview of Module Loading

Modules in Java are resolved from the module path, which is distinct from the traditional classpath. At the startup of the Java Virtual Machine (JVM), a graph of modules is resolved. This graph must be acyclic, meaning it cannot contain any circular dependencies. One module acts as the root of this graph and contains the main method that is executed. This structure ensures a clear and organized dependency management system, facilitating better modularization and encapsulation of code.

Platform Modules

Platform modules are an integral part of the modular Java Development Kit (JDK). These modules can be listed using the --list-modules flag, which provides a comprehensive view of all available platform modules. Platform modules utilize qualified exporting, meaning they can selectively expose their packages to other modules. An essential platform module is java.base, which is fundamental to all Java applications as it contains the core classes and interfaces of the Java language.

Application Modules

Application modules are resolved from the module path and represent the modularized dependencies of an application, or the application itself. These modules can depend on both platform modules and other application modules. By organizing application code into modules, developers can manage dependencies more effectively and ensure that only the necessary parts of the code are exposed to other modules.

Automatic Modules

Automatic modules provide a bridge for nonmodular JAR files to be used in a modular environment. By placing nonmodular JAR files onto the module path, they are automatically converted into modules. These automatic modules export all their packages and have dependencies on all other modules present in the module path. This feature allows for a smoother transition from nonmodular to modular codebases, enabling developers to incrementally adopt the module system without needing to immediately refactor all existing JAR files into explicit modules.