Course contentsShow
← Java
Lesson 2550 of 2,87047. Java Platform Module System (JPMS)Free lesson

Compression and Optimization Options

Using --compress, --strip-debug, and --no-header-files to minimize runtime image size further.

Compression and Optimization Options

You'll learn how to shrink your custom runtime images dramatically using jlink's built-in compression and optimization flags.


When you create a custom runtime image with jlink, the resulting package can still be quite large because it includes debug information, header files, and uncompressed binaries. Java provides three powerful flags to slim down your runtime image further: --compress, --strip-debug, and --no-header-files.

The --compress Flag

This option compresses internal resources in the runtime image. You can specify compression levels:

  • --compress=0 (no compression)
  • --compress=1 (constant string sharing)
  • --compress=2 (ZIP compression, maximum space savings)

Higher compression means smaller images but slightly longer build times.

The --strip-debug Flag

Debug information embedded in class files helps developers troubleshoot, but production runtimes rarely need it. This flag removes debug symbols, line numbers, and local variable information, reducing image size by 20-30% typically.

The --no-header-files Flag

By default, jlink includes C/C++ header files for JNI development. If your runtime image is for deployment only (not development), this flag excludes those headers, saving additional space.

Combining Options

jlink --module-path $JAVA_HOME/jmods:mods \
      --add-modules myapp \
      --output myapp-runtime \
      --compress=2 \
      --strip-debug \
      --no-header-files \
      --launcher myapp=myapp/com.example.Main

Think of it like packing a suitcase: you remove unnecessary items (debug info, headers), then vacuum-seal everything (compression) to fit more efficiently.

Key Takeaway: Use --compress=2, --strip-debug, and --no-header-files together with jlink to create production-ready runtime images that are 40-60% smaller than default builds.