Sub topic 16
Limitations and trade-offs
This chapter assumes you can build a modular application, run jdeps, and create an image with jlink, as covered in the earlier chapters. It looks at what a custom runtime costs you and where it breaks, so you can decide when it is worth using.
What you gain and what you take on
A jlink image is smaller than a full JDK, starts from a known set of modules, and ships with your application. In exchange, you become the maintainer of a Java runtime. Nothing on the target machine updates it, and no package manager knows it exists.
Four costs follow from that:
- Patching. Security fixes to the JDK reach your users only when you rebuild and redeploy the image.
- Fixed module set. Any module you leave out is missing at run time, and nothing downloads it.
- Platform lock-in. An image runs only on the operating system and CPU architecture of the JDK that supplied its
jmods. - Modular input only.
jlinklinks modules. Code that is not a proper module needs extra handling.
Missing modules fail at run time
jdeps reads bytecode references. It cannot see a class that your code loads by name at run time. Code that does this can pass your build and then fail in production, because the module is not in the image.
You can reproduce this safely. The first program tries to load classes by name and reports which ones it finds.
public class Probe {
public static void main(String[] args) {
for (String name : args) {
try {
Class.forName(name);
System.out.println("found " + name);
} catch (ClassNotFoundException e) {
System.out.println("missing " + name);
}
}
}
}Compile it with the full JDK, build an image that contains only java.base, and run the probe on that image.
mkdir -p out/probe
javac -d out/probe Probe.java
jlink --module-path "$JAVA_HOME/jmods" \
--add-modules java.base \
--output build/min
build/min/bin/java -cp out/probe Probe \
java.lang.String java.util.logging.Logger java.sql.ConnectionThe output shows that only java.base classes are present:
found java.lang.String
missing java.util.logging.Logger
missing java.sql.ConnectionThe same thing happens with frameworks that load drivers, serializers, or plugins by name. The fix is to add the module yourself with --add-modules, even though jdeps did not report it.
Some modules are easy to forget:
| Module | Typically needed for | Symptom when absent |
|---|---|---|
| `jdk.localedata` | Locales other than English and a few others | Dates and numbers format with fallback locale data |
| `jdk.unsupported` | Libraries using `sun.misc.Unsafe` | `NoClassDefFoundError` at start-up of the library |
| `java.sql` | JDBC drivers loaded by name | `ClassNotFoundException` for `java.sql.*` |
| `jdk.crypto.ec` | Elliptic-curve TLS on older JDKs | TLS handshake failures with some servers |
| `jdk.jcmd`, `jdk.jfr` | Production diagnostics | `jcmd` and Flight Recorder are not in the image |
The jdk.crypto.ec entry applies to older JDKs. Newer releases moved that functionality into java.base, so check your own JDK version.
Non-modular code is the hardest limit
jlink refuses automatic modules, which are plain JARs that the module path treats as modules. It stops with an error that names the module. Many real applications depend on such JARs, so this limit comes up often.
You have these options:
- Use a dependency version that ships a real
module-info.class. - Generate a descriptor for the JAR yourself with
jdeps --generate-module-info. Then compile it and repackage the JAR. This works for simple libraries and breaks on libraries with split packages or reflective access. - Skip
jlinkfor the dependencies. Build an image for the JDK modules only, and put the application and its JARs on the classpath. This approach is common and works, with one caveat: you must work out the JDK module list yourself.
For option 3, jdeps can produce the list directly from the classpath:
jdeps --print-module-deps --ignore-missing-deps \
--class-path 'lib/*' app.jar--ignore-missing-deps suppresses errors about classes that are not in the analysed JARs. It also hides real problems, so read the output with that in mind.
Signed JARs are another limit. jlink rejects signed modular JARs, because linking changes the packaging that the signature covers. Signed dependencies need to be unsigned or replaced before linking.
Services need a decision
By default, jlink includes only the modules that your root modules require. A service provider that is reachable only through ServiceLoader is not required by anything, so it is left out. The code runs and finds no provider.
--bind-services adds every provider module for the services that the included modules use. It fixes the missing provider, but it can pull in many modules you did not intend to ship, and the image grows. A narrower approach is to add the specific provider modules with --add-modules. This keeps the contents predictable and reviewable.
Size savings have limits
--strip-debug, --no-header-files, --no-man-pages, and --compress reduce the image, and they carry costs:
--strip-debugremoves debug attributes from the classes in the image. Stack traces from those classes may lose line numbers. Keep a debug-enabled image for staging if you rely on them.--compresstrades disk space for a little start-up and class-loading work. The accepted values depend on the JDK version. Older releases use0,1, and2, and newer releases usezip-0tozip-9. Checkjlink --helpon your JDK.- Removing diagnostic modules saves space and makes production problems harder to investigate.
The module set matters far more than the flags. An image with java.base only is small. Every extra module adds to it, and java.desktop is a large one. Measure with du -sh build/runtime before and after a change, and compare the result with what you saved against what you now maintain.
Maintenance practices that keep the cost low
Treat the image as a build artifact that is rebuilt often. These habits keep the cost manageable:
- Pin the JDK version used to build the image, and record it. The generated
releasefile in the image holds version metadata you can inspect. - Rebuild on every JDK security release, even if your own code did not change.
- Build the image in CI, not on a developer machine, so that the
jmodsdirectory is the same every time. - Keep a short list of extra modules (reflection, locales, diagnostics) in the build script with a comment for each, so a later maintainer knows why it is there.
- Build one image per target platform, using the matching JDK for each.
When not to use jlink
Skip jlink when you deploy to a managed platform that supplies and patches the JVM. Skip it when your application depends on many non-modular libraries and you cannot test every code path against a trimmed module set. Skip it when image size does not matter. In those cases the full JDK is simpler, and its cost is only disk space.
Exercise
Your application passes jdeps --print-module-deps and builds an image successfully. In production, a feature that loads a JDBC driver by class name throws ClassNotFoundException for a java.sql class. Why did jdeps not catch this, and what is the fix?
Show answer
jdeps analyses static references in bytecode. A class loaded by name through Class.forName or a similar mechanism is a string at analysis time, so jdeps does not report the module that contains it. The fix is to add the module explicitly with --add-modules java.sql when running jlink, and to run integration tests against the image so such gaps appear before release.
Exercise
A teammate wants to add --bind-services to make a missing ServiceLoader provider appear in the image. Name one risk, and one narrower alternative.
Show answer
The risk is that --bind-services adds every provider module for the services in use, which grows the image and adds modules you must patch and review. The narrower alternative is to name only the needed provider module in --add-modules, then confirm the result with java --list-modules.