Sub topic 7
Compiling and Running on the Module Path
This chapter assumes you know Java packages, javac, java, JAR files, and basic classpath usage. You will compile a small multi-module application and run it with the module path.
Source Layout
A modular source tree usually places each module in its own directory. The directory name should match the module name when using --module-source-path.
src/
├── com.example.greeter/
│ ├── module-info.java
│ └── com/example/greeter/api/Greeter.java
└── com.example.app/
├── module-info.java
└── com/example/app/Main.javaThe greeter module exports its public package:
// src/com.example.greeter/module-info.java
module com.example.greeter {
exports com.example.greeter.api;
}
// src/com.example.greeter/com/example/greeter/api/Greeter.java
package com.example.greeter.api;
public class Greeter {
public String greet(String name) {
return "Hello, " + name;
}
}The application module requires the greeter module:
// src/com.example.app/module-info.java
module com.example.app {
requires com.example.greeter;
}
// src/com.example.app/com/example/app/Main.java
package com.example.app;
import com.example.greeter.api.Greeter;
public class Main {
public static void main(String[] args) {
System.out.println(new Greeter().greet("modules"));
}
}requires makes the greeter module readable to the application. The exports directive makes the API package accessible.
Compile with `javac`
Create an output directory, then compile the application module:
mkdir -p out
javac -d out --module-source-path src -m com.example.appThe -d out option selects the destination directory. The --module-source-path src option tells javac where to find module source directories. The -m option selects the module to compile. The compiler follows its requires directives and compiles the required module too.
The output keeps module boundaries:
out/
├── com.example.app/
└── com.example.greeter/Run with `java`
Use the output directory as a module path:
java --module-path out \
-m com.example.app/com.example.app.MainThe --module-path option identifies directories or JAR files containing named modules. The -m option uses the form module/main-class.
| Option | Purpose |
|---|---|
| `--module-source-path src` | Finds module source code |
| `--module-path out` | Finds compiled modules |
| `-m com.example.app` | Selects a module for compilation |
| `-m com.example.app/com.example.app.Main` | Selects a module and its main class |
Common Errors
module not found: Check the module path and the module name inrequires.package ... is not visible: The package may not be exported, or the consuming module may not read its module.Could not find or load main class: Check both parts of themodule/main-classvalue.module not in module graph: Confirm that the selected module is present in the module path.
Exercise
Which command runs com.example.app.Main from the compiled module com.example.app in the out directory?
Show answer
java --module-path out -m com.example.app/com.example.app.Main