Sub topic 18
Building a custom runtime image with jlink
This chapter covers jlink, the JDK tool that assembles a trimmed Java runtime from the modules your application needs. It assumes you can write a module-info.java, compile on the module path, and read requires and provides directives. You should also know what jdeps reports, because it feeds the inputs to jlink.
Why build a custom runtime
A full JDK contains about seventy modules, including compilers, debuggers, and desktop and management libraries. A typical service uses a small part of that. Shipping the whole JDK has costs:
- Larger container and installer images.
- A bigger attack surface, because unused modules still ship code.
- A dependency on whatever JDK the target machine happens to have installed.
jlink takes a set of root modules, follows their requires directives, and writes a self-contained runtime image. The image holds only the resolved modules, a java launcher, and optional launcher scripts for your application. The target machine needs no separate JDK or JRE.
The basic command
The command below builds an image for the com.example.app module from earlier chapters.
jlink --module-path "$JAVA_HOME/jmods:out" \
--add-modules com.example.app \
--launcher app=com.example.app/com.example.app.Main \
--output image
image/bin/appEach option has a distinct role:
| Option | Purpose |
|---|---|
| `--module-path` | Where to find modules. It must include the JDK's `jmods` directory plus your compiled modules or modular jars. |
| `--add-modules` | The root modules. `jlink` resolves everything they require. |
| `--launcher` | Creates a script `name=module/mainclass` in `bin`. |
| `--output` | The directory to create. It must not already exist. |
The jmods directory is a JDK feature. It holds modules in the JMOD format, which jlink reads. Some JDK distributions omit it, and then jlink fails to find java.base. Check that $JAVA_HOME/jmods exists.
Finding the right modules with jdeps
Writing --add-modules by hand is simple when your application is fully modular. For a classpath application, jdeps can suggest the JDK modules to include:
jdeps --print-module-deps app.jarThe output is a comma-separated list such as java.base,java.sql. You can pass it directly:
jlink --module-path "$JAVA_HOME/jmods" \
--add-modules "$(jdeps --print-module-deps app.jar)" \
--output imageUse jdeps with care. It analyzes static references in bytecode. It cannot see modules that your code loads through reflection or ServiceLoader at run time, so those must be added by hand.
Services and `--bind-services`
jlink resolves requires edges but, by default, does not pull in modules that only provide a service. Suppose com.example.greeter declares uses GreetingProvider and com.example.greeter.french provides it. If you add only the first module, the French provider is missing from the image, and ServiceLoader finds nothing.
You have two options. The first is to name the provider explicitly:
jlink --module-path "$JAVA_HOME/jmods:out" \
--add-modules com.example.greeter,com.example.greeter.french \
--output imageThe second is --bind-services, which adds every provider module found on the module path for each service used by the resolved modules:
jlink --module-path "$JAVA_HOME/jmods:out" \
--add-modules com.example.greeter \
--bind-services \
--output image--bind-services can pull in more than you expect. It also adds JDK provider modules, which can enlarge the image a lot. Naming providers explicitly gives a smaller, more predictable result.
Size and startup options
jlink includes plugins that shrink the image. Run jlink --list-plugins to see the set your JDK supports. These are the common ones:
jlink --module-path "$JAVA_HOME/jmods:out" \
--add-modules com.example.app \
--launcher app=com.example.app/com.example.app.Main \
--strip-debug \
--no-header-files \
--no-man-pages \
--compress=zip-6 \
--output image--strip-debugremoves debug information from classes. Stack traces lose line numbers and local variable names, so use it only when you do not need them.--no-header-filesand--no-man-pagesleave out files that applications do not need at run time.--compresscompresses resources. Higher levels save space but add decompression work at startup. Older JDKs use numeric levels (--compress=2), and newer ones accept names such aszip-6. Checkjlink --helpfor your version.
Inspecting the result
Confirm what ended up in the image:
image/bin/java --list-modules
image/bin/java --describe-module com.example.app
du -sh imageIf a module you expected is absent, it was not reachable from the root modules. If an unexpected one is present, trace which requires or bound service brought it in.
Limits and failure modes
- Automatic modules are rejected.
jlinkrequires explicit modules. If your module requires a legacy jar placed on the module path as an automatic module,jlinkreports an error. Fix it by making the dependency a real module. You can add amodule-info.classto the jar withjar --updateorjdeps --generate-module-info, then repackage it. - Classpath code cannot be linked. Code in the unnamed module has no descriptor, so
jlinkcannot include it. Everything in the image must be a named module. - The image is platform specific. It contains native launchers and libraries from the JDK that supplied
jmods. To build for Linux, use a Linux JDK, or point--module-pathat thejmodsof a JDK for the target platform. - Reflection still needs
opens.jlinkdoes not change encapsulation rules. A framework that reflects on your classes needs the package opened. - Updates are your job. The image bundles its own JDK code. Security patches to the JDK require you to rebuild the image.
Exercises
Exercise
You build an image with --add-modules com.example.greeter, and at run time ServiceLoader returns no providers, although com.example.greeter.french is on the module path. Why, and how can you fix it?
Show answer
jlink follows requires edges only. com.example.greeter.french provides the service but nothing requires it, so it is not resolved into the image. Fix it by adding it to --add-modules, or by passing --bind-services so jlink adds provider modules for the services in use.
Exercise
jlink fails with an error that a dependency is an automatic module. What are the two ways to proceed?
Show answer
Make the dependency an explicit module by adding a module-info.class to its jar (for example, generated with jdeps --generate-module-info), or replace it with a modular version of the library. jlink does not accept automatic modules or classpath code.
Exercise
Why can jdeps --print-module-deps give an incomplete module list for --add-modules?
Show answer
jdeps analyzes static bytecode references. Modules loaded only by reflection or by ServiceLoader at run time do not appear, so you must add them manually.