jlink tool assembles and optimizes a set of modules and their dependencies into a custom runtime image. It is part of the jdk.jlink module.
Basic Usage
Source Code
The jlink implementation is located at:- Main task:
src/jdk.jlink/share/classes/jdk/tools/jlink/internal/JlinkTask.java:1 - Task helper:
src/jdk.jlink/share/classes/jdk/tools/jlink/internal/TaskHelper.java:1 - Image builder:
src/jdk.jlink/share/classes/jdk/tools/jlink/builder/DefaultImageBuilder.java:1
Required Options
Path where the jlink tool discovers observable modules. Can also use
-p.The path can be a directory containing module files or a module file itself. Multiple paths are separated by the platform-specific path separator (: on Linux/macOS, ; on Windows).Root modules to resolve in addition to the initial module. The modules must be found in the module path.
Location of the generated runtime image. The directory must not already exist.
Module Selection Options
Limit the universe of observable modules to those in the comma-separated list, plus the main module (if any), plus any further modules that these modules depend upon.
Suggest providers that implement the given service type from the module path.
Link service provider modules and their dependencies.
Image Configuration
Add a launcher command of the given name for the module, and optionally the main class.
Byte order of the generated image (default: native).
Suppress a fatal error when signed modular JARs are linked in the image. The signature-related files of the signed modular JARs are not copied to the runtime image.
Output Control
Enable verbose tracing. Can also use
-v.Save jlink options in the specified file.
Plugin Options
jlink includes a plugin system for customizing the generated image. Use--list-plugins to see available plugins.
- Compression
- Strip Debug
- VM
- Include Locales
- Order Resources
compress
Compress resources in the output image.Level 0: No compression Level 1: Constant string sharing Level 2: ZIP compressionCommon Usage Examples
Create Minimal Java Runtime
Create Application Runtime
Create Optimized Production Runtime
Create Cross-Platform Runtime
Include Service Providers
Advanced Options
Preserve packaged modules in the specified directory in the output image.
Custom plugin module path.
Information Options
Print help message. Can also use
-h or -?.Print version information.
List available plugins.
Runtime Image Structure
The generated runtime image has the following structure:The
modules file in the lib/ directory is a custom format optimized for fast module lookup and loading. It contains all the modules specified with --add-modules and their dependencies.When using
--launcher, jlink creates a native launcher that directly starts your application without requiring java -m modulename/mainclass. This provides a better user experience for standalone applications.Plugin Development
Custom plugins can be created by implementing thejdk.tools.jlink.plugin.Plugin interface:
Exit Codes
0: Successful operation1: Errors occurred2: Invalid command-line arguments3: System error or resource exhaustion