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

Basic jlink Command Syntax

The jlink command structure: --module-path, --add-modules, and --output options for creating runtime images.

Basic jlink Command Syntax

What you'll learn: How to structure the jlink command with its essential options to create custom Java runtime images.

Now that you understand what jlink does and its prerequisites, let's explore how to actually use it. The jlink command assembles modules into a self-contained runtime image, and its syntax revolves around three fundamental options.

Core Command Structure

The jlink command follows this basic pattern:

jlink --module-path <path> --add-modules <modules> --output <directory>

The Three Essential Options

--module-path (or -p): Specifies where jlink should look for modules. This typically includes both your application modules and the JDK's module path. You're pointing jlink to the "parts warehouse" containing all available modules.

--module-path $JAVA_HOME/jmods:mods

--add-modules (or --add-module): Tells jlink which modules to include as starting points. jlink will resolve dependencies transitively, pulling in everything these modules require. Think of this as your "shopping list" of root modules.

--add-modules com.example.myapp

--output: Specifies the directory where the custom runtime image will be created. This is your destination folder—jlink will create the complete directory structure here, including bin, lib, and conf subdirectories.

--output myapp-runtime

Complete Example

jlink --module-path $JAVA_HOME/jmods:target/mods \
      --add-modules com.example.myapp \
      --output myapp-runtime

This command creates a runtime image in the myapp-runtime directory containing com.example.myapp and all its transitive dependencies.

Key Takeaway: The jlink command requires three core pieces: where to find modules (--module-path), which modules to include (--add-modules), and where to create the runtime image (--output)—together they build a minimal, custom Java runtime tailored to your application's needs.