Sub topic 8
What is inside the image
This chapter assumes you can run the jlink command from the previous chapter and have the build/runtime image it produced. You should also know what a module descriptor is. Here you will look at what jlink wrote to disk, what each part is for, and which parts you can safely ignore.
The top-level layout
A runtime image is an ordinary directory. It has no installer and no registry entries. Listing it shows a small set of directories:
find build/runtime -maxdepth 1 | sort
du -sh build/runtimeOn Linux or macOS with the image from the previous chapter, you will see roughly:
| Path | Contents |
|---|---|
| `bin/` | The `java` executable, any tools from included modules, and your generated launchers |
| `conf/` | Configuration files, such as `logging.properties` and `security/java.security` |
| `lib/` | The `modules` file, native libraries, and the JVM itself in `lib/server/` |
| `legal/` | License files, one directory per included module |
| `release` | A text file describing the image |
| `include/` | JNI headers. Present only without `--no-header-files` |
| `man/` | Manual pages. Present only without `--no-man-pages` |
The exact files vary with the JDK version, the vendor, the operating system, and the modules you included. On Windows, native libraries such as DLLs sit in bin/, and launchers are .bat files.
lib/modules: where the classes live
The most important file is lib/modules. It contains the compiled classes and resources of every module in the image, stored in the jimage format. This format is built for fast loading by the JVM. You cannot open it with unzip, and it is not a JAR.
The jmods files you passed with --module-path are not copied into the image. jlink reads them, takes the classes, native libraries, and configuration files out of them, and writes them into the image's own layout. Your application's modular JAR is handled the same way. After the build, the image does not need mods/app.jar or $JAVA_HOME/jmods any more.
The JDK ships a jimage tool for looking inside this file:
jimage list build/runtime/lib/modules | head -n 20The output groups entries under Module: lines, one per module in the image. jimage comes from your full JDK, not from the image. Options such as --strip-debug and --compress change what is stored inside this one file, so the file's size is the main thing they affect.
Code in the image can also read this content at run time through the jrt:/ file system. Tools such as jshell and some frameworks use it. This is why the image only needs lib/modules and not thousands of loose class files.
The release file
release is a plain KEY="value" text file. jlink generates it and merges in values from the JDK's own release file:
cat build/runtime/releaseTypical output looks like this, with values that depend on your JDK:
IMPLEMENTOR="Eclipse Adoptium"
JAVA_VERSION="17.0.9"
MODULES="java.base java.logging com.example.app"
OS_ARCH="x86_64"
OS_NAME="Linux"Two lines matter most in practice:
MODULESlists every module in the image after dependency resolution. It containsjava.baseeven though you never asked for it, because every module requires it.JAVA_VERSION,OS_NAME, andOS_ARCHrecord the platform the image was built for. If you see support tickets about an image failing on another machine, check these values first.
Other tools and scripts can read this file to find out what an image contains without starting a JVM.
bin/: the java executable and launchers
bin/java is a normal java launcher, but it can only see the modules in lib/modules. If your image has no java.sql module, code that uses JDBC fails with a ClassNotFoundException or a NoClassDefFoundError even if the full JDK on the same machine has it.
The launcher you requested with --launcher hello=... is a small script next to java. On Linux and macOS it looks like this:
cat build/runtime/bin/hello#!/bin/sh
JLINK_VM_OPTIONS=
DIR=`dirname $0`
$DIR/java $JLINK_VM_OPTIONS -m com.example.app/com.example.app.Main "$@"The script runs the image's own java with -m <module>/<main class>. Because it starts from $DIR, it works from any working directory and does not use JAVA_HOME or the PATH. Arguments pass through with "$@". You can read the script, so there is nothing hidden about how the application starts.
Other tools appear in bin/ only if their modules are included. For example, keytool comes from java.base, so it is usually there. jar, javac, and jdeps live in jdk.* modules that your image does not contain unless you add them.
conf/ and lib/security
conf/ holds files that the JDK reads at start-up or on demand. Examples are logging.properties (from java.logging), net.properties, and security/java.security. They are copied from the JDK and are editable. If your application needs a different logging setup, you can change conf/logging.properties in the image, or better, pass -Djava.util.logging.config.file=... from the launcher.
This means an image is not fully immutable. Anyone with write access can change these files. In container builds, mark the image directory read-only or treat the image as a build output you never patch by hand.
Inspecting the module set
The release file tells you what jlink wrote. To check what the runtime actually resolves, ask the image:
build/runtime/bin/java --list-modules
build/runtime/bin/java --describe-module java.logging--list-modules prints every observable module with its version, for example [email protected]. The --describe-module command prints the exports and requirements of one module. If --list-modules shows a module that is missing from MODULES, something is wrong with the image. The two lists should agree.
Common surprises
- The image is bigger than expected. Look at
du -sh build/runtime/lib/modulesfirst. A single service-providing module can pull in many others when you use--bind-services. - A tool you expect is missing. Add the module that provides it with
--add-modules, for examplejdk.jartoolforjar. - The image works on your machine but not on a colleague's. Compare
OS_NAMEandOS_ARCHinrelease. The native parts inlib/are platform-specific. - Debug information is gone. After
--strip-debug, stack traces and debuggers lose some information about local variables, which is the cost of the smaller image. - Headers or man pages are absent. This is intended when you used
--no-header-filesand--no-man-pages. Nothing is broken.
Exercise
Exercise
You built an image with --add-modules com.example.app, but release shows MODULES="java.base java.logging com.example.app". You never mentioned java.base or java.logging on the command line. Why do they appear, and where in the image can you confirm that the classes are present?
Show answer
jlink resolves the dependencies of the modules you name. com.example.app declares requires java.logging, and every module implicitly requires java.base, so both are added. The classes are stored in lib/modules, which you can inspect with jimage list build/runtime/lib/modules. You can also run build/runtime/bin/java --list-modules to see what the image resolves.
Exercise
A teammate's script runs build/runtime/bin/jar and fails because the file does not exist. What caused this, and how would you fix it?
Show answer
The jar tool comes from the jdk.jartool module, which the image does not include because the application does not depend on it. Rebuild the image with --add-modules com.example.app,jdk.jartool. Alternatively, run jar from the full JDK during the build and leave it out of the runtime.