14.2 Module Resolution Explained
Module resolution in Java SE 11 is the process by which the Java runtime determines which modules are required to run an application and how they are loaded. This process ensures that all dependencies are satisfied and that the application can run correctly. Understanding module resolution is crucial for managing dependencies and ensuring the correct behavior of modular applications.
Key Concepts
1. Module Path
The module path is a list of directories or JAR files that the Java runtime searches for modules. When an application requires a module, the runtime looks for it in the module path. If the module is found, it is loaded; otherwise, an error occurs.
Example
java --module-path lib --module com.example.myapp/com.example.myapp.Main
2. Automatic Modules
Automatic modules are JAR files placed on the module path that are not explicitly defined as modules. The Java runtime automatically infers the module name and dependencies from the JAR file. This allows legacy JAR files to be used in a modular application without modification.
Example
java --module-path lib --module com.example.myapp
3. Module Resolution Process
The module resolution process involves several steps:
- The runtime starts with the initial module (the main application module).
- It recursively resolves all required modules, checking the module path for each dependency.
- If a required module is not found, the resolution process fails, and the application cannot start.
- Once all dependencies are resolved, the runtime loads the modules and their classes.
Example
module com.example.myapp { requires java.base; requires java.logging; }
4. Module Graph
The module graph is a visual representation of the dependencies between modules. Each node in the graph represents a module, and each edge represents a dependency. The module graph helps in understanding the structure of the application and identifying potential issues, such as cyclic dependencies.
Example
com.example.myapp ├── java.base └── java.logging
5. Module Resolution Errors
Module resolution errors occur when a required module is not found or when there is a conflict between modules. Common errors include "module not found" and "module conflict." These errors must be resolved to ensure the application can run.
Example
Error: Module com.example.missing not found
Examples and Analogies
Think of module resolution as assembling a puzzle. Each piece (module) has specific connections (dependencies) to other pieces. The module path is the table where you look for the pieces. If a required piece is missing, the puzzle cannot be completed. The module graph is the blueprint that shows how all the pieces fit together.
For instance, if you are building a house, each room (module) has specific materials (dependencies) that it needs. The module path is the warehouse where you find these materials. If a required material is missing, the room cannot be built. The module graph is the architectural plan that shows how all the rooms connect.
By mastering module resolution, you can ensure that your Java SE 11 applications are correctly assembled, with all dependencies satisfied, leading to reliable and maintainable software.