Phase 12 of 20 · Topic 12.5

Classpath vs Modulepath Execution Mechanics

1Concept

Legacy Java relies on the Classpath (`-cp`), which has no encapsulation (all public classes visible everywhere) and causes 'JAR Hell'. The Modulepath (`--module-path` or `-p`) verifies that all required modules are present before JVM startup and enforces strict package boundary encapsulation.

2Architecture Diagram

Classpath (-cp):      All JARs dumped into one flat global bucket (No version/dependency checking!)
Modulepath (-p):      Strict directed acyclic graph (DAG) of modules verified at startup

3Code Example

Core Java
public class ModulePathInspection {
    public static void main(String[] args) {
        ModuleLayer bootLayer = ModuleLayer.boot();
        long moduleCount = bootLayer.modules().size();
        System.out.println("=== JVM Runtime Module Metrics ===");
        System.out.println("Total Loaded Boot Modules: " + moduleCount);
        System.out.println("Runtime Platform: " + System.getProperty("java.vm.version"));
    }
}

4Expected Output

=== JVM Runtime Module Metrics ===
Total Loaded Boot Modules: 72
Runtime Platform: 21.0.2+13-LTS

5Key Takeaways

  • Modules eliminate NoSuchMethodError and ClassNotFoundException at runtime via startup validation.
  • Use `jlink` tool to strip unused modules and build custom minimal JVM runtimes under 35MB.
  • Non-modular JARs on the classpath are treated as part of the 'unnamed module'.