Module Path Introduction: A New Way to Organize Code
You'll learn what the module path is, how it replaces the classpath for modular applications, and why Java 9 introduced this new mechanism.
What Is the Module Path?
The module path is a new way to specify where the JVM should look for modules at compile-time and runtime. While the classpath searches for individual .class files and JARs in a flat structure, the module path works with modular JARs that contain module-info.class descriptors.
Think of it this way: the classpath is like throwing all your books into one big pile—you can find what you need, but there's no organization. The module path is like organizing those books into labeled boxes (modules), where each box declares exactly what it contains and what it needs from other boxes.
Key Differences from Classpath
Organization: The classpath treats all code as one giant pool with no boundaries. The module path recognizes distinct modules with explicit dependencies.
Encapsulation: On the classpath, all public types are accessible to everyone. On the module path, only explicitly exported packages are accessible.
Reliability: The classpath allows duplicate classes and missing dependencies to fail at runtime. The module path validates the module graph at startup, catching errors early.
Why It Was Introduced
Java 9 introduced the module path to solve major classpath problems: unclear dependencies, accidental access to internal APIs, and runtime failures from missing JARs. The module path enforces strong encapsulation and reliable configuration, making large applications more maintainable and secure.
You specify the module path with --module-path (or -p) instead of --classpath.
Key Takeaway: The module path is Java's modern alternative to the classpath, organizing code into well-defined modules with explicit dependencies and strong encapsulation, eliminating many runtime reliability issues.