Sub topic 7
Compiling and running on the module path
This chapter assumes you can compile and run programs with javac and java, and that you know what a jar file is. It also assumes you have read the earlier chapters on module-info.java, requires, and exports. Here you will turn those descriptors into a working build: compile several modules together, run them, package them as jars, and diagnose the errors you will meet first.
Classpath versus module path
The classpath is a flat list of jars and directories. The JVM searches it in order for each class it needs. The module path is a list of places that contain modules. The runtime reads each module's name and descriptor before it loads any class, so it can check that every requires is satisfied at startup.
A module path entry can be one of three things:
- An exploded module: a directory with
module-info.classat its top level. - A modular jar: a jar with
module-info.classat its root. - A directory whose entries are modular jars or exploded modules.
Entries are separated by : on Linux and macOS and by ; on Windows. If two entries contain a module with the same name, the first one found wins. Two modules with the same name inside one directory entry are an error.
Source layout for a multi-module build
The --module-source-path option expects one directory per module, named after the module:
src/
com.example.greeter/
module-info.java
com/example/greeter/api/Greeter.java
com.example.app/
module-info.java
com/example/app/Main.javaCompiling everything at once
javac -d out --module-source-path src -m com.example.appThe -m option names the module to compile. The compiler also finds com.example.greeter in the source path, because com.example.app requires it, and compiles it too. The -d out option writes each module into its own subdirectory:
out/
com.example.greeter/module-info.class ...
com.example.app/module-info.class ...Each subdirectory is an exploded module, so out is a valid module path entry. Run the program like this:
java --module-path out -m com.example.app/com.example.app.MainThe short forms are -p and -m. The argument to -m has the form module/mainclass. You can drop /mainclass only when the module's jar records a main class (see below).
Compiling one module against jars
In a real project you often compile one module against dependencies that are already built. In that case you pass the dependencies with --module-path and give javac the source files, including module-info.java:
javac -d out/com.example.app \
--module-path mods/com.example.greeter.jar \
$(find src/com.example.app -name "*.java")Note that --module-source-path is for compiling modules from source, and --module-path is for compiled dependencies. Mixing them up is a frequent cause of "module not found".
Packaging modular jars
The jar tool turns each compiled module into a jar. It can also store a main class in the jar's metadata:
mkdir -p mods
jar --create --file mods/com.example.greeter.jar -C out/com.example.greeter .
jar --create --file mods/com.example.app.jar \
--main-class com.example.app.Main \
-C out/com.example.app .
java -p mods -m com.example.app
jar --describe-module --file mods/com.example.app.jarBecause the app jar records its main class, -m com.example.app is enough. The --describe-module option prints the module name, what it requires, and what it exports. The java launcher accepts the same option, as java --describe-module java.base.
How the runtime resolves modules
When you launch with -m, the named module is the root module. The runtime then resolves the module graph in these steps:
- Start from the root modules.
- Look up each
requiresin the module path, then in the JDK's system modules. - Add the dependencies found, and repeat for theirs.
- Fail at startup if any required module is missing or if the graph is ambiguous.
Some modules are not reachable through requires, such as a service provider that nobody requires. Add these with --add-modules. Three options help you see what was resolved:
java --list-modules
java -p mods --add-modules com.example.greeter.french -m com.example.app
java -p mods --show-module-resolution -m com.example.appCommon errors
| Message | Cause | Fix |
|---|---|---|
| `error: module not found: com.example.greeter` (javac) | The module is on neither the module source path nor the module path | Add `--module-source-path` or `--module-path` |
| `package com.example.greeter.internal is not visible` | The package is not exported, or the module does not require its owner | Add `exports` or `requires` |
| `FindException: Module com.example.greeter not found, required by com.example.app` (java) | The jar is missing from the module path at run time | Fix the `-p` value |
| `package com.example.greeter.api does not exist` | Dependency jar is on `-cp`, not `-p` | Put it on the module path |
The second error comes in two forms. If the message says the package "is declared in module X, which does not export it", fix X's descriptor. If it says "module Y does not read it", add requires X to Y.
Edge cases
- Directories of classes on the classpath are fine for old code, but a named module found only on the classpath loses its module identity. It becomes part of the unnamed module, which a later chapter covers.
- Module names are matched exactly. A typo in
requiressurfaces as a "module not found" error, not as a warning. - The module path is read once at startup. Adding jars to a directory while the JVM runs does not change the resolved graph.
Exercise
You run java -p out -m com.example.app/com.example.app.Main and get FindException: Module com.example.greeter not found, required by com.example.app. The file out/com.example.greeter/module-info.class exists. Which two causes are most likely, and how do you check?
Show answer
The most likely causes are that the working directory is not the one containing out, so the relative path points nowhere, and that com.example.greeter was never compiled into out (for example, it was compiled to a different -d directory). Check with java -p out --list-modules, which shows every module the launcher finds on that path. If com.example.greeter is missing from the output, fix the path or recompile with javac -d out --module-source-path src -m com.example.app.