Sub topic 11
Checking that the image works
This chapter assumes you can compile modular Java code, have built an image with jlink (see "Your first jlink image"), and know that an image has bin, lib, conf and a release file. Here you will check that the image contains what you expect and that the application runs on it.
Why verify
jlink succeeds as long as the modules you named can be resolved. It does not run your code. A missing module, such as one reached only through reflection, shows up only at run time. Verification catches these problems before you ship.
Run three checks, from cheapest to most thorough:
- List the modules in the image.
- Read the
releasefile. - Run the application through the image's own launcher.
Check 1: list the modules
The image has its own java executable. Ask it which modules it contains:
build/runtime/bin/java --list-modulesFor the example application, the output looks like this. The version suffix depends on your JDK.
com.example.app
[email protected]
[email protected]Compare this list with what you expected from jdeps. You should see java.base, java.logging and your own module, and nothing large that you did not ask for. An unexpected module such as java.desktop points to a dependency you did not know about.
You can also describe one module to see what it requires:
build/runtime/bin/java --describe-module java.loggingCheck 2: read the release file
The release file at the image root records metadata that jlink wrote:
cat build/runtime/releaseIt contains key-value lines such as JAVA_VERSION, OS_NAME, OS_ARCH and MODULES. Use it to confirm the Java version and the target platform. This matters most when you build images for several systems.
Check 3: run the application
Run the launcher you generated with --launcher:
build/runtime/bin/helloThe output should include the log line from the application:
INFO: Hello from a custom runtimeYou can also start the module directly with the image's java:
build/runtime/bin/java -m com.example.app/com.example.app.MainIf both work, the launcher script and the module path inside the image are correct.
Make sure you test the image, not your JDK
A common mistake is testing with the java on your PATH. That runs on the full JDK and hides missing modules. Always call build/runtime/bin/java by its path, or clear PATH for the test. To be strict, run the test on a machine that has no JDK installed.
Reading failures
| Symptom | Likely cause | Fix |
|---|---|---|
| `Module java.sql not found` | The module was not added to the image | Add it with `--add-modules` and rebuild |
| `ClassNotFoundException` for a class loaded by name | Reflection or a service provider that `jdeps` could not see | Add the providing module, or use `--bind-services` |
| `Error: Could not find or load main class` | Wrong module or class name in `--launcher` | Check `module/class` spelling |
| Launcher runs but exits with `Permission denied` | The script lost its executable bit when copied | Restore it with `chmod +x` |
Exercise
Your image builds without errors and java -m com.example.app/com.example.app.Main works on your laptop, but fails in the image with a missing-module error. What did you probably run, and how do you check it properly?
Show answer
You probably ran the java from your full JDK, which has every module. Run build/runtime/bin/java instead, and compare its --list-modules output with the modules the application needs.