Modular JARs vs Non-Modular JARs at Runtime
You'll learn: How the JVM handles the runtime interaction between modular and non-modular JARs, and the concept of automatic module conversion.
The Runtime Challenge
When you deploy a Java application, you might have a mix of modular JARs (with module-info.class) and traditional non-modular JARs on your paths. The JVM needs a strategy to make them work together without breaking existing code.
Automatic Modules: The Bridge
When a non-modular JAR is placed on the module path (not classpath), the JVM automatically converts it into an automatic module. Think of it like giving a passport to someone who crossed a border—they're allowed in, but with special rules.
Key Characteristics of Automatic Modules:
- Module name derived: From the JAR filename (or
Automatic-Module-Namein MANIFEST.MF if present) - Exports everything: All packages are implicitly exported
- Reads all modules: Automatically
requiresevery other module it can see - Reflection-friendly: All packages are open for deep reflection
This means your old, pre-JPMS libraries can still participate in the module system without modification.
The Four Runtime Scenarios
- Modular JAR on module path: Normal modular behavior—strong encapsulation applies
- Non-modular JAR on module path: Becomes an automatic module
- Non-modular JAR on classpath: Joins the "unnamed module" (pre-JPMS behavior)
- Modular JAR on classpath: Module descriptor is ignored; treated as non-modular
Why This Matters
This dual-mode system enables gradual migration. Your fully modular code can depend on automatic modules (legacy libraries), allowing you to modernize incrementally rather than rewriting everything at once.
Key Takeaway: Non-modular JARs on the module path become automatic modules that export all packages and read all other modules, bridging the gap between legacy code and JPMS while maintaining compatibility.