Sub topic 5
The module path and the jmods directory
This chapter assumes you can compile and run a modular program with javac and java, and that you know what a module-info.java file declares. Earlier chapters covered the module descriptor and the JDK's module names. Here we look at where those modules come from when jlink builds an image.
What the module path is
The classpath is a flat list of places to look for classes. The module path is a list of places to look for modules. Each entry has a module name, a descriptor, and a set of packages. You set it with --module-path or the short form -p. The separator between entries is : on Linux and macOS and ; on Windows.
An entry on the module path can be any of these:
| Entry | What the tools do with it |
|---|---|
| A modular JAR (contains `module-info.class`) | The JAR is one module. |
| An exploded module directory (contains `module-info.class` at its top) | The directory is one module. |
| A `.jmod` file | The file is one module. Only `javac` and `jlink` can read it. |
| A directory holding any of the above | Each child that is a module becomes a module. |
The last row is the one you will use most. Passing mods means "every module found directly inside mods". It does not mean "a module named mods".
If the same module name appears in two separate entries, the first entry wins. If it appears twice inside one directory, the tools report an error. Because order matters, put your own entries and the JDK's entries in a deliberate order, and avoid shipping a module that has the same name as a JDK module.
Observable modules and resolution
The set of modules the tools can see is called the observable modules. For jlink it is the union of everything found on --module-path. The tool does not take everything it can see. It starts from the root modules you name with --add-modules, then follows each requires clause transitively. The modules it reaches form the image.
If a module is observable but nothing reaches it, it stays out of the image. If a required module is not observable, the build fails. That second case is the most common first error with jlink.
The jmods directory
A JDK installation has a jmods directory next to bin and lib. It holds one .jmod file per JDK module, such as java.base.jmod and java.logging.jmod. The exact list depends on the JDK version and vendor.
ls "$JAVA_HOME/jmods" | head
jmod describe "$JAVA_HOME/jmods/java.logging.jmod"
jmod list "$JAVA_HOME/jmods/java.logging.jmod" | head -n 20jmod describe prints the module descriptor, including its requires and exports. jmod list prints the file's contents. Entries are grouped under section prefixes such as classes/, which shows that a JMOD file is more than a JAR.
Why JMOD and not JAR
A JAR holds classes and resources. A JDK module also needs other things that belong in a runtime image:
- Native commands, such as the
javalauncher, which sits in thebinsection ofjava.base.jmod. - Native libraries, such as the
.so,.dllor.dylibfiles a module loads. - Configuration files, which end up in the image's
confdirectory. - Header files and man pages.
jlink copies each section to the matching place in the image. The options --no-header-files and --no-man-pages exist because those sections are in the JMOD files. They are not in the classes. A JAR has no such sections, which is why the JDK ships its modules as JMOD files.
The runtime's own view of its modules
A running JDK does not read the jmods directory. Its modules are stored in an internal format inside the runtime, in lib/modules. You can list them with java --list-modules, and describe one with java --describe-module java.logging.
You can also list them from code with ModuleFinder.ofSystem().
import java.lang.module.ModuleDescriptor;
import java.lang.module.ModuleFinder;
import java.lang.module.ModuleReference;
public class ListSystemModules {
public static void main(String[] args) {
ModuleFinder.ofSystem().findAll().stream()
.map(ModuleReference::descriptor)
.map(ModuleDescriptor::name)
.sorted()
.forEach(System.out::println);
}
}Run it with java ListSystemModules.java. The output should match java --list-modules apart from version suffixes. The two views come from different places. jmods is the input for building images. The system modules are what a built image contains. A full JDK shows both, and a jlink image has only the second.
Putting the JDK and your modules together
For jlink, the module path must contain your modules and the JDK's modules. The JDK's jmods directory is the source of the JDK part.
Here is the command from the example application, with each part of the path labeled:
jlink \
--module-path "$JAVA_HOME/jmods:mods" \
--add-modules com.example.app \
--output build/runtimeThe path has two entries. $JAVA_HOME/jmods supplies java.base and java.logging. mods supplies com.example.app from app.jar. The root is com.example.app. That module requires java.logging, and every module requires java.base, so the image ends up with those three modules plus whatever java.logging requires in turn.
The default module path
If you do not pass --module-path, jlink uses the jmods directory of the JDK it belongs to. That works for an image that contains only JDK modules, for example jlink --add-modules java.logging --output tiny. As soon as you pass --module-path to add your own modules, you replace the default. You must then list the JDK's jmods yourself, as in the command above. Forgetting this is the usual cause of a "module not found" error that names java.base or java.logging.
Version matching
Use the jlink and jmods from the same JDK build. The jmods directory's java.base module records hashes of the other JDK modules, and jlink checks them. If you mix files from different builds, the link step fails with a hash mismatch. Pointing JAVA_HOME at one JDK and the path at another is how this usually happens.
Common errors
| Symptom | Likely cause | Fix |
|---|---|---|
| `jlink` reports a missing module such as `java.base` or `java.logging` | `--module-path` was set without the JDK's `jmods` directory | Add `$JAVA_HOME/jmods` to the path |
| `jlink` reports your module is missing | The directory holding the JAR is not on the path, or the JAR has no `module-info.class` | Pass the directory or JAR, and check with `jar --describe-module --file mods/app.jar` |
| Hash mismatch for a JDK module | `jmods` and `jlink` come from different builds | Use both from one JDK |
| The path is parsed as a single entry | The wrong separator for the operating system | Use `:` on Linux and macOS, `;` on Windows |
| The `java` launcher rejects a `.jmod` file | JMOD files cannot be used at run time | Use a modular JAR |
| `jlink` rejects an automatic module | A plain JAR with no descriptor was put on the path | See the chapter on non-modular code |
When there is no jmods directory
Some distributions leave out jmods to save space, and some Linux packages put it in a separate package. In that case you must install the package or use a full JDK download. Starting with JDK 24, a JDK built with the right option can link images directly from its own runtime, without JMOD files (JEP 493). Whether that works depends on the vendor build. Check with your distribution before relying on it, and keep the jmods method as the default in your build scripts.
Exercise
A build script runs jlink --module-path mods --add-modules com.example.app --output build/runtime and fails because java.base cannot be found. What is wrong, and what is the corrected command on Linux?
Show answer
Passing --module-path replaces the default path, which is the JDK's jmods directory. The path now contains only mods, so java.base is not observable. Add the jmods directory: jlink --module-path "$JAVA_HOME/jmods:mods" --add-modules com.example.app --output build/runtime.
Exercise
Why does the JDK ship its modules as .jmod files when it could ship JARs, and what can you not do with a .jmod file?
Show answer
A JMOD file can hold native commands, native libraries, configuration files, header files and man pages in addition to classes, and jlink copies each of these to the right place in the image. You cannot use a .jmod file on the module path of the java launcher. It works only at compile time and link time.