Sub topic 5
The module path and the jmods directory
This chapter assumes you can compile a modular project with javac, know what module-info.java declares, and know the difference between the classpath and the module path.
Two places jlink looks
When you run jlink, it must find every module that goes into the image. It looks only on the module path, which is the list of directories and modular JARs you pass with --module-path (short form -p). It does not use the classpath.
The module path for a typical build has two parts:
- The
jmodsdirectory of a JDK, which supplies the JDK's own modules. - A directory with your application's modular JARs.
On Linux and macOS the entries are separated by a colon. On Windows they are separated by a semicolon.
What is in the jmods directory
The jmods directory sits under $JAVA_HOME. It holds one .jmod file per JDK module, such as java.base.jmod and java.logging.jmod.
A JMOD file is a packaging format for build time. It resembles a JAR, but it can also carry native libraries, command-line tools, configuration files, and header files. You cannot put a JMOD file on the runtime module path or classpath. Only jlink and the jmod tool use it.
You can inspect these files with the jmod tool:
ls "$JAVA_HOME/jmods" | head
jmod list "$JAVA_HOME/jmods/java.logging.jmod"
jmod describe "$JAVA_HOME/jmods/java.logging.jmod"jmod describe prints the module's name, its exports, and its requires. This is the same information module-info.java gives for your own code.
Putting the path together
The jlink command from the previous chapters used this value:
--module-path "$JAVA_HOME/jmods:mods"Here mods is the directory that holds app.jar. Because jlink resolves modules from this combined path, anything named in --add-modules or required by those modules must be present in one of the two entries.
Common errors
- Module not found. If you leave out
jmods, or misspell a module name, jlink reports that the module cannot be found. Check the path and the--add-modulesvalue first. - Wrong path separator. A colon on Windows, or a semicolon in a Unix shell, makes the whole value look like one entry. The shell may also treat an unquoted semicolon as a command separator, so quote the value.
- Mismatched JDK versions. Use the
jmodsdirectory from the same JDK that provides thejlinkyou run. The image's modules come from those files, so mixing versions causes errors or an image for the wrong release. - No jmods directory. Some JDK distributions do not ship
jmods. Install a full JDK package, or check whether your JDK supports linking without JMOD files (JEP 493, available from JDK 24 in builds that enable it).
Exercise
Your jlink command fails because it cannot find module java.sql, and your module path is only mods. What is the likely cause, and what is the fix?
Show answer
The JDK modules come from the jmods directory, which is missing from the module path. Add it, for example --module-path "$JAVA_HOME/jmods:mods", and make sure it belongs to the same JDK as the jlink you run.