Sub topic 18
Building a Custom Runtime Image with `jlink`
Assume you know Java modules, module-info.java, javac, java, and the module path. You should also know that a JAR file packages classes, while a runtime image contains a Java runtime and selected application modules.
Building a Runtime Image
A normal Java installation contains many modules that an application may not use. jlink creates a smaller runtime image containing only the modules needed by your application and its dependencies.
Suppose com.example.app has already been compiled into out. Its module descriptor names the application's dependencies:
module com.example.app {
requires com.example.greeter;
}Build an image with the JDK's standard modules and your compiled modules on the module path:
jlink --module-path "$JAVA_HOME/jmods:out" \
--add-modules com.example.app \
--launcher app=com.example.app/com.example.app.Main \
--output imageThe --module-path option supplies module definitions. The JDK modules are stored in the jmods directory. The out directory contains your compiled modules.
The --add-modules option selects the application's root module. jlink follows its requires declarations and includes the modules reachable from that root. The --launcher option creates a convenient executable named app. The resulting image is stored in the image directory.
Run the application without using the original JDK installation:
image/bin/appYou can inspect the image with commands such as:
image/bin/java --list-modules
image/bin/java --describe-module com.example.appThe image is usually smaller and easier to deploy. However, it is specific to an operating system and processor architecture. Build a separate image for each target environment.
Module Layers and Dynamic Loading
A module layer is a runtime view of a module graph. The boot layer contains the modules used to start the application. You can create another layer to load plugins or optional modules after startup.
import java.lang.module.Configuration;
import java.lang.module.ModuleFinder;
import java.nio.file.Path;
import java.util.Set;
public class LoadPlugin {
public static void main(String[] args) throws Exception {
ModuleLayer parent = ModuleLayer.boot();
ModuleFinder finder = ModuleFinder.of(Path.of("mods"));
Configuration configuration = parent.configuration().resolve(
finder, ModuleFinder.of(), Set.of("com.example.plugin"));
ModuleLayer layer = parent.defineModulesWithOneLoader(
configuration, ClassLoader.getSystemClassLoader());
ClassLoader loader = layer.findLoader("com.example.plugin");
Class<?> type = loader.loadClass("com.example.plugin.Plugin");
Object plugin = type.getConstructor().newInstance();
System.out.println(plugin);
}
}The plugin module must be present in mods, and its package must export the class being loaded. A new layer has its own module graph and class loader, which helps isolate plugins and allows different module sets to coexist.
Exercise
Why does jlink include modules required by the root module instead of copying every module from the JDK?
Show answer
It follows the module dependency graph so the image contains the selected application and the modules it needs, while avoiding unrelated modules.