Sub topic 16
Limitations and trade-offs
This chapter assumes you can build an image with jlink, read jdeps output, and know what module-info.java does. It covers what a custom runtime costs you, so you can decide when it is worth using.
You now own the runtime
A full JDK installed on a server is patched by the operating system or a package manager. A jlink image is a copy of the JDK modules at build time. Nothing updates it afterward. When the JDK vendor ships a security fix, you must rebuild the image and redeploy it.
Treat the JDK version as a build input, like a library version. Pin it, record it, and rebuild on every vendor update. The release file in the image shows which JDK produced it.
Platform lock-in
An image contains native code for one operating system and CPU architecture. An image built on Linux x64 does not run on Windows or on macOS. To target another platform, you need the jmods of a JDK for that platform. In practice, most teams build one image per target platform in CI.
Non-modular code
jlink only links named modules. If your application depends on a plain JAR with no descriptor, the JAR becomes an automatic module when placed on the module path. jlink refuses to link automatic modules.
You have three options:
- Use a library version that ships a
module-info.class. - Add a descriptor yourself with
jdeps --generate-module-info. - Keep the library on the classpath and ship it next to the image.
What jdeps cannot see
jdeps reads bytecode references. It misses modules that your code reaches only at run time, for example:
- A class loaded by name with
Class.forName. - A JDBC driver found through a service lookup.
- A charset or locale provider that the JDK finds on its own.
The image builds without error, and then the application fails with ClassNotFoundException or a missing provider in production. Run the real application on the image, not only --list-modules. For service providers, consider --bind-services, which adds more modules than you may want.
Smaller images, harder debugging
--strip-debug removes debug information from the JDK classes. Stack traces and profiler output inside JDK code then carry less detail. An image also has no javac, jcmd, or jmods unless you add those modules. This makes live diagnosis harder. If you need jcmd or jfr on a server, include jdk.jcmd or jdk.jfr and accept the extra size.
Trade-off summary
| Concern | Full JDK or JRE | jlink image |
|---|---|---|
| Size | Large | Small |
| Patching | Managed by the OS | You rebuild and redeploy |
| Portability | Install per platform | One build per platform |
| Non-modular libraries | Works on the classpath | Needs workarounds |
| Missing modules | Rare | Possible at run time |
Exercise
Your jlink build passes and java --list-modules looks right, but the deployed application fails with a missing JDBC driver. What most likely happened, and what are two ways to fix it?
Show answer
The driver is found through a service lookup, so jdeps did not see it and the module was not linked. Fix it by adding the module explicitly with --add-modules, or by using --bind-services so service provider modules are included. Then test the application on the image.