7.1 Module System Overview
The Java Platform Module System (JPMS) introduced in Java 9 is a significant enhancement to the Java programming language. It provides a modular approach to building and maintaining large-scale applications, making it easier to manage dependencies, improve security, and optimize performance. Understanding the Module System is crucial for developing robust and maintainable Java SE 11 applications.
Key Concepts
1. Modules
A module is a self-contained unit of code that encapsulates a set of related packages, classes, and resources. Modules help in organizing code into manageable and reusable components. Each module has a name and a descriptor file named module-info.java
that defines its dependencies, exported packages, and services.
Example
module com.example.mymodule { requires java.base; exports com.example.mymodule.api; }
2. Module Path
The module path is a new concept that replaces the traditional classpath. It specifies the location of module definitions and dependencies. The module path allows the Java runtime to locate and load modules, ensuring that only the required modules are loaded, which improves performance and security.
Example
java --module-path mods --module com.example.mymodule/com.example.mymodule.Main
3. Automatic Modules
Automatic modules are a way to integrate existing JAR files into the module system without requiring any modifications. These JAR files are treated as modules with their names derived from the JAR filenames. Automatic modules can access all other modules on the module path, making it easier to migrate legacy code to the module system.
Example
java --module-path lib/mylibrary.jar --module com.example.mymodule/com.example.mymodule.Main
4. Named Modules
Named modules are explicitly defined modules with a module-info.java
descriptor. They provide strong encapsulation, allowing only explicitly exported packages to be accessed by other modules. Named modules are the recommended approach for new Java applications to ensure better modularity and maintainability.
Example
module com.example.mymodule { requires java.base; exports com.example.mymodule.api; }
5. Unnamed Modules
Unnamed modules are a fallback mechanism for legacy code that does not use the module system. All classes loaded from the classpath are treated as part of an unnamed module. Unnamed modules can access all other modules, but their classes are not encapsulated and can be accessed by any other module.
Example
java -classpath lib/legacy.jar com.example.legacy.Main
Examples and Analogies
Think of a module as a toolbox containing specific tools (packages and classes) that can be used to build a project. Each toolbox (module) has a label (module-info.java) that describes what tools are inside and what other toolboxes it depends on. The module path is like a shelf where all the toolboxes are stored, and the Java runtime knows exactly which toolbox to pick up based on the project requirements.
By mastering the Module System, you can build scalable and maintainable Java SE 11 applications, ensuring that your code is organized, secure, and optimized for performance.