Sub topic 7
Your first jlink image
This chapter builds a runtime image from a small modular application and explains what each part of the jlink command does. It assumes you can compile and run Java programs with javac and java, and that you know what a JAR and the classpath are. It also assumes you have read the earlier chapters on module-info.java, the JDK's modules, and jdeps. You need a JDK 11 or later (not only a JRE) whose installation includes the jmods directory.
What jlink does
jlink is a JDK tool that assembles a runtime image. You give it a set of root modules. It resolves their dependencies, copies only those modules from the module path into a new directory, and adds a JVM and the supporting files needed to run them. The result is a directory you can copy to another machine of the same platform and run without a separate Java installation.
Three options carry most of the work in a basic invocation:
| Option | Meaning |
|---|---|
| `--module-path` | Where jlink looks for modules. It must include the JDK's `jmods` directory and the directory or JAR holding your application modules. |
| `--add-modules` | The root modules. jlink adds these and everything they require, directly or transitively. |
| `--output` | The directory to create. It must not already exist. |
The example application
The application has one module that depends on java.logging, so the image needs more than java.base.
src/module-info.java:
module com.example.app {
requires java.logging;
}src/com/example/app/Main.java:
package com.example.app;
import java.util.logging.Logger;
public class Main {
private static final Logger LOG = Logger.getLogger(Main.class.getName());
public static void main(String[] args) {
LOG.info("Hello from a custom runtime");
}
}Compile the module and package it as a modular JAR. Then ask jdeps which JDK modules the JAR needs:
mkdir -p out/com.example.app mods
javac -d out/com.example.app \
src/module-info.java src/com/example/app/Main.java
jar --create --file mods/app.jar \
--main-class com.example.app.Main \
-C out/com.example.app .
jdeps --print-module-deps mods/app.jarjdeps prints java.base,java.logging. You do not need to pass these to jlink yourself. Because com.example.app declares requires java.logging, jlink finds both during resolution. jdeps matters most when you have to confirm what a third-party JAR needs. For a modular application, the descriptor already tells jlink everything.
Running jlink
Run the command from the project directory. The $JAVA_HOME/jmods directory holds the JDK's own modules in .jmod format.
jlink \
--module-path "$JAVA_HOME/jmods:mods" \
--add-modules com.example.app \
--output build/runtimeOn Windows, separate the module-path entries with a semicolon instead of a colon, and use %JAVA_HOME%\jmods;mods.
Here is what happens during that run:
- jlink builds the module graph. It starts at
com.example.app, follows itsrequirestojava.logging, then follows that module'srequirestojava.base. - It locates each of those modules on the module path.
modssupplies the application, andjmodssupplies the two JDK modules. - It writes the modules into a single optimized file under
build/runtime/lib, copies thejavalauncher and other required files, and records the module list in areleasefile.
The graph contains three modules, and nothing else from the JDK is copied. The JDK has dozens of modules, including java.sql, java.desktop, and java.xml. None of them are in the image because nothing in your graph requires them.
Running the application from the image
You did not ask for a custom launcher, so use the image's java command and name the module and main class:
build/runtime/bin/java --module com.example.app/com.example.app.MainThe output looks like this, with your own timestamp:
Nov 26, 2026 9:16:27 AM com.example.app.Main main
INFO: Hello from a custom runtimeThe java command in the image is not the one on your PATH. It only knows the modules inside the image. You can test that by running build/runtime/bin/java --list-modules, which should show com.example.app, java.base, and java.logging. The later chapters on layout and verification cover this in more detail.
If you leave out --module-path for the application modules, or pass --add-modules with a name that is not on the path, jlink stops with a message that the module was not found. The runtime is never partly built, so a successful run means resolution succeeded.
Rules and common errors
The output directory must not exist
jlink refuses to write into a directory that already exists. Rebuilding therefore needs a delete step first:
rm -rf build/runtime
jlink \
--module-path "$JAVA_HOME/jmods:mods" \
--add-modules com.example.app \
--output build/runtimeBe careful with rm -rf in scripts. Keep the path fixed and relative, and never build it from an unchecked variable.
Missing `jmods`
Some JDK distributions, such as certain Linux packages, ship jmods in a separate package. Others omit it. If $JAVA_HOME/jmods is absent or empty, jlink cannot find java.base and fails. Check it exists with ls "$JAVA_HOME/jmods" before debugging anything else. A JRE-only install cannot run jlink at all.
The image is tied to the JDK that built it
The runtime image contains the JVM and class library copied from the JDK that supplied jmods. It is not portable across operating systems or CPU architectures, and it is not updated when you update the JDK on the machine. If a security update is released for your JDK, you have to rebuild the image to pick it up.
Match the compiler and the runtime
Compile with the same JDK feature release that supplies jmods, or an older one. Code compiled by a newer javac may refer to classes the older runtime lacks. Using one JDK for javac, jar, jdeps, and jlink avoids the mismatch.
Non-modular JARs
jlink rejects automatic modules, which are plain JARs placed on the module path. If your application depends on one, resolution fails with an error naming it. Handling that case is covered in the chapter on non-modular code, so this example sticks to a fully modular application.
Summary of the workflow
- Write
module-info.javawith therequiresclauses the code needs. - Compile, and package a modular JAR in a directory such as
mods. - Optionally check dependencies with
jdeps. - Run
jlinkwith a module path of$JAVA_HOME/jmodsplus your modules directory, one or more--add-modulesroots, and a fresh--output. - Run the application with the image's
bin/java.
The next chapters inspect the directory structure that jlink produced, add a named launcher, and shrink the image.
Exercise
You run jlink --module-path mods --add-modules com.example.app --output build/runtime and it fails because it cannot find java.base. What is missing from the command, and why does the image still not need you to list java.logging in --add-modules?
Show answer
The module path lacks the JDK's jmods directory, so it should be --module-path "$JAVA_HOME/jmods:mods" (with ; on Windows). java.logging does not need to be listed because com.example.app declares requires java.logging. jlink follows requires clauses from the root modules and adds every module it finds, including java.base.
Exercise
You run the same jlink command twice in a build script and the second run fails, although nothing in the sources changed. What is the likely cause and what is the fix?
Show answer
The --output directory (build/runtime) already exists from the first run, and jlink does not overwrite existing directories. Delete the directory before running jlink, for example rm -rf build/runtime, or write to a fresh directory each time.