Sub topic 3
Making the Image Smaller
Assume you can compile Java code, create JAR files, and use the command line. A runtime image contains the Java modules needed to run an application, rather than a complete JDK.
Remove Unneeded Files
After creating an image, remove development files with these options:
jlink \
--module-path "$JAVA_HOME/jmods:mods" \
--add-modules com.example.app \
--launcher hello=com.example.app/com.example.app.Main \
--strip-debug \
--no-header-files \
--no-man-pages \
--compress=2 \
--output build/runtime--strip-debugremoves debugging information.--no-header-filesremoves native header files.--no-man-pagesremoves manual pages.--compress=2compresses resources more aggressively.
Compression can reduce size but may increase startup or extraction work. Measure the result for your application.
Check That the Image Works
A runtime image is platform-specific and should be tested like any other build artifact.
build/runtime/bin/java --list-modules
build/runtime/bin/hello
cat build/runtime/releaseThe first command shows included modules. The second runs the generated launcher. The release file records runtime metadata, such as the Java version.
Services and `--bind-services`
Java services use a service interface and provider modules discovered at runtime. If your application relies on service providers, add --bind-services:
jlink \
--module-path "$JAVA_HOME/jmods:mods" \
--add-modules com.example.app \
--bind-services \
--output build/runtimeThis includes provider modules reachable through the service-binding mechanism. Without it, a provider may be absent even though the application starts successfully during ordinary development.
Non-Modular Code
jlink links named modules. A non-modular application can still use a custom image if the application JARs remain on the classpath and the image contains their required JDK modules. Use jdeps --print-module-deps as a starting point, then test reflective and dynamically loaded dependencies manually. Automatic modules and classpath libraries are not a substitute for a fully modular application when reliable linking is required.
Other Platforms, Containers, and CI
Build the image with the JDK and jmods directory for the target operating system and CPU architecture. A Linux image is not a Windows image. In Docker or CI, use a fixed JDK image, run jdeps and jlink in the build stage, and copy only the application and runtime image into the smaller final stage.
> callout note > Keep the JDK version used to build an image aligned with the version you test and support.
Limitations and Trade-Offs
Custom images reduce download size and avoid requiring users to install Java. They also add maintenance work. Rebuild images for security updates, test every target platform, and retain the build commands or container definition so the image can be reproduced.
Exercise
Why should --bind-services be used only when service providers are needed?
Show answer
It can add provider modules that are unnecessary for applications without service-based dependencies, increasing image size.