Running Modular Applications: --module-path Flag
You'll learn how to launch modular Java applications by specifying where modules are located using the --module-path flag.
Now that you understand the module path concept, it's time to actually use it when running your applications. The --module-path (or its shorthand -p) flag tells the java command where to find your modules at runtime—just like -classpath pointed to JARs and directories in the pre-JPMS world.
The Basic Syntax
When running a modular application, you specify the module path and then tell Java which module (and main class) to execute:
java --module-path <path-to-modules> --module <module-name>/<main-class>
Or using the shorthand:
java -p <path-to-modules> -m <module-name>/<main-class>
What Goes on the Module Path?
The --module-path accepts:
- Directories containing modular JARs
- Directories containing exploded module directories (with
module-info.classat the root) - Individual modular JAR files
Multiple locations can be separated by your platform's path separator (: on Unix/Linux/Mac, ; on Windows).
java --module-path lib:mods -m com.example.app/com.example.Main
Why It Matters
Unlike the classpath, which dumps all classes into a flat namespace, the module path maintains strong encapsulation and enforces the module graph you've declared with requires and exports. The JVM uses the module path to resolve dependencies and verify your module declarations at startup.
Key Takeaway:
Use --module-path (or -p) with the java command to specify where your modules live, enabling the JVM to resolve and enforce your modular application's dependency graph at runtime.