Sub topic 11
Checking that the image works
This chapter assumes you can build the build/runtime image from the previous chapters and that you know what java.base, jdeps, and the module path are. Here you will verify an image in layers, from cheapest check to most realistic, and learn to read the failures each layer catches.
Why a successful jlink run proves little
jlink exits with status 0 when it can resolve the modules you asked for and write the image. It does not know what your code does at run time. These problems all survive a clean build:
- Code that loads a class by name with
Class.forNameneeds a module that norequiresclause mentions. - A library uses reflection or
ServiceLoaderto find a provider that was not linked in. - The launcher script was generated with the wrong module or class name.
- The image was built from a different JDK than the one you tested with.
So you check the image directly. Each check below catches a different class of mistake.
Layer 1: inspect what was linked
An image has its own java executable in bin. Always call that one, not the java on your PATH. Otherwise you test your installed JDK and learn nothing about the image.
build/runtime/bin/java --list-modulesFor the example application, the output looks like this. Version numbers depend on your JDK.
com.example.app
[email protected]
[email protected]Three things to read from this output:
java.baseis always present, because every image needs it.java.loggingis there becausemodule-info.javasaysrequires java.logging.- Your own module has no version, because the example JAR was built without
--module-version.
If the list holds modules you did not expect, jlink followed a requires chain you did not know about. If a module is missing, the dependency was never declared or never resolved.
Next, read the release file, which jlink writes at the image root.
cat build/runtime/releaseIt holds KEY="value" lines. The useful ones are JAVA_VERSION and MODULES. JAVA_VERSION tells you which JDK the image came from. MODULES lists the linked modules and is easy to parse in a script.
To see what a single module offers and needs, use --describe-module:
build/runtime/bin/java --describe-module java.logging
build/runtime/bin/java --describe-module com.example.appThe output lists requires, exports, and any provides or uses lines. This is a quick way to confirm that the module in the image is the one you built and not a stale copy.
Layer 2: validate module resolution
The java launcher can resolve every module in the image and exit without running anything.
build/runtime/bin/java --validate-modulesNo output and exit status 0 means the module graph is consistent. This catches conflicts such as two modules exporting the same package.
You can also ask the launcher to show how the boot layer is resolved:
build/runtime/bin/java --show-module-resolution -m com.example.app/com.example.app.MainEach line shows a module being resolved and which module required it. The output is noisy, but it explains why a module is in the image. That helps when --list-modules shows something surprising.
Layer 3: run the application
Run the launcher that jlink generated with --launcher:
build/runtime/bin/helloExpected output is two lines from java.util.logging. The timestamp and the class and method line vary.
Nov 26, 2026 6:58:15 AM com.example.app.Main main
INFO: Hello from a custom runtimeThen run the same program through the image's java with an explicit module. This separates "the launcher is wrong" from "the application is wrong."
build/runtime/bin/java -m com.example.app/com.example.app.MainIf the second command works and the first fails, look at the --launcher name=module/class argument you passed to jlink. If both fail, the problem is in the image content.
You can also confirm which runtime executed the code:
build/runtime/bin/java -versionThe vendor and version strings should match the JAVA_VERSION in release.
Layer 4: exercise the real code paths
A "hello" program touches almost nothing. Real applications fail on paths that the smoke test never runs. Before you ship, run something that uses the features customers use. For example, run your integration tests against the image's java, or start the server and call its main endpoints.
Two failure patterns are common.
A missing JDK module. The symptom is NoClassDefFoundError or ClassNotFoundException for a JDK class, such as java.sql.Connection, long after startup. The cause is usually reflection or a library that needs a module your code never names. You can reproduce the symptom on purpose. Ask the image for a module it does not contain:
build/runtime/bin/java --add-modules java.sql -m com.example.app/com.example.app.MainThe launcher fails at startup with a FindException saying that module java.sql was not found. At startup that is a clear error. A reflective load at run time gives you the less obvious ClassNotFoundException.
A missing service provider. ServiceLoader quietly returns nothing when no provider is linked in. Your program may then fall back to a default or fail much later. Compare the uses lines from --describe-module with the providers present in --list-modules. Service binding is covered in a later chapter.
Automate the checks
Put the cheap checks in a script so CI can run them after every build. This script fails if a required module is missing, if resolution is invalid, or if the application produces unexpected output.
#!/usr/bin/env bash
set -euo pipefail
IMAGE="${1:-build/runtime}"
REQUIRED_MODULES=(java.base java.logging com.example.app)
modules="$("$IMAGE/bin/java" --list-modules)"
for m in "${REQUIRED_MODULES[@]}"; do
if ! grep -qE "^${m}(@|$)" <<<"$modules"; then
echo "FAIL: module $m missing from image" >&2
exit 1
fi
done
"$IMAGE/bin/java" --validate-modules
output="$("$IMAGE/bin/hello" 2>&1)"
if ! grep -q "Hello from a custom runtime" <<<"$output"; then
echo "FAIL: unexpected application output" >&2
echo "$output" >&2
exit 1
fi
echo "OK: image passed verification"
echo "Size: $(du -sh "$IMAGE" | cut -f1)"The grep pattern ^name(@|$) matches a module name at the start of a line, followed by either a version marker or the end of the line. A plain substring match would let java.logging falsely satisfy a check for a module named java.log.
The last line reports image size. Keeping that number in your CI logs shows when a new dependency quietly adds tens of megabytes.
Common mistakes
| Symptom | Likely cause | What to check |
|---|---|---|
| `bin/hello: No such file or directory` | Launcher not requested at link time, or it was misspelled | `ls build/runtime/bin` |
| `Module X not found` at startup | Module was not linked into the image | `--list-modules`, then add it with `--add-modules` |
| Works on your machine, fails in the image | You ran the system `java` instead of `build/runtime/bin/java` | Use the full path |
| `ClassNotFoundException` for a JDK class mid-run | Reflection-only dependency | Add the module, then add a test that covers the path |
| Feature silently does nothing | Service provider not linked | `--describe-module` and `--bind-services` |
| `JAVA_VERSION` differs from expectation | Built from the wrong `jmods` directory | `release` file and `$JAVA_HOME` |
Summary of the checks
Use --list-modules, release, and --describe-module to confirm content. Use --validate-modules to confirm the module graph. Run the generated launcher and java -m to confirm startup. Run real workloads to catch reflection and service gaps. Keep the first three as an automatic gate in CI and treat the fourth as part of your normal test suite.
Exercise
Your image passes --list-modules, --validate-modules, and a hello-world run. In production, the application throws ClassNotFoundException: java.sql.Driver when it first connects to a database through a library that loads the driver by name. Why did the earlier checks not catch it, and what would you change?
Show answer
The static checks only see modules that were linked or declared. jdeps and the module descriptor cannot see a class name that a library loads by a computed string at run time, so java.sql was never added to the image, and the hello-world run never reached that code path. Add java.sql to the --add-modules list (or add requires java.sql to the application's module descriptor) and rebuild. Then add a test that opens a database connection using the image's bin/java, and add java.sql to the module list that the CI verification script requires.
Exercise
A teammate says the image works because java -version printed the expected vendor. Which command did they probably run, and what should they run instead?
Show answer
They probably ran the java on their PATH, which belongs to an installed JDK. They should run build/runtime/bin/java -version and compare it with JAVA_VERSION in build/runtime/release.