Sub topic 7
Your first jlink image
This chapter assumes you can compile and package a small modular Java application, and that you know what requires does in a module-info.java file. It also assumes a JDK 9 or later with the jmods directory, and that JAVA_HOME points to it. You will build a runtime image that holds only the modules the application needs.
What jlink does
jlink takes a set of root modules, resolves everything they require, and copies those modules into a new directory. That directory is a complete Java runtime. It has its own java executable and does not need a JDK installed on the target machine.
The application
The module declares one dependency beyond java.base.
module com.example.app {
requires java.logging;
}The class uses that dependency.
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 and check the dependencies
Compile the module, package it as a modular JAR, and ask jdeps which JDK modules it uses.
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.jarThe last command should print java.base,java.logging. It confirms that the module descriptor and the code agree.
Create the image
jlink \
--module-path "$JAVA_HOME/jmods:mods" \
--add-modules com.example.app \
--launcher hello=com.example.app/com.example.app.Main \
--strip-debug --no-header-files --no-man-pages \
--output build/runtimeOn Windows, separate the module-path entries with a semicolon instead of a colon.
Three options are required:
| Option | Purpose |
|---|---|
| `--module-path` | Where jlink looks for modules. It lists the JDK's `jmods` directory and the folder holding your JAR. |
| `--add-modules` | The root modules. jlink adds every module they require, directly or indirectly. |
| `--output` | The directory to create. It must not already exist. |
The remaining options add a launcher named hello and trim the image. Later chapters cover them in detail.
Run the image
build/runtime/bin/java --list-modules
build/runtime/bin/helloThe first command lists a small set of modules, including com.example.app, java.logging and java.base. The second runs the application and prints the log message.
Common errors
- Module not found. jlink cannot find a module named in
--add-modulesorrequires. Check that--module-pathincludes both$JAVA_HOME/jmodsand the folder with your JAR, and that the module name matches the one inmodule-info.java. - Output directory already exists. jlink does not overwrite. Delete
build/runtimeor pick a new path before running it again. - Missing JDK module at run time. If you build the image from a hand-written module list instead of
requires, the application may fail withNoClassDefFoundErrorfor a class in a module you left out. Runjdepsagain and compare.
Exercise
You run jlink with --add-modules com.example.app, but your module-info.java also says requires java.sql;. Do you need to add java.sql to --add-modules? Why or why not?
Show answer
No. jlink resolves the dependencies of each root module, so it includes java.sql and the modules that java.sql requires. You only list extra modules that no root module requires.