Modular JARs on the Module Path
You'll learn: How to place modular JARs on the module path so the Java module system can discover and resolve them properly.
The Module Path vs Classpath
When working with JPMS, you use the module path instead of the traditional classpath. The module path is where you tell the Java runtime and compiler to look for modules. When you place a modular JAR on the module path, the module system reads its module-info.class, discovers its name, dependencies (requires), and exported packages, then builds a module graph.
Think of it like this: The classpath was a jumbled pile of classes where everything was visible to everything. The module path is an organized library catalog where each module declares exactly what it needs and what it shares.
Using --module-path
You specify the module path with the --module-path (or -p) option:
# Compiling with modules on the module path
javac --module-path mods -d out --module-source-path src -m mymodule
# Running with modules on the module path
java --module-path mods -m mymodule/com.example.Main
You can list multiple directories or JARs, separated by the platform path separator (: on Linux/Mac, ; on Windows):
java --module-path lib:mods:other-libs -m mymodule/com.example.Main
How Resolution Works
When you specify --module-path, the module system:
- Scans each location for modular JARs and exploded module directories
- Reads each
module-info.classto identify module names - Builds the module graph by resolving
requiresdependencies - Validates that all required modules are present and acyclic
If a required module is missing, you'll get a clear error at startup—no more ClassNotFoundException at random runtime moments!
Key Takeaway: Use --module-path (or -p) to tell Java where your modular JARs live; the module system then reads their descriptors and resolves dependencies into a validated module graph before your application even starts.