Java Modules (JPMS): Build and Run ·
‹ Previous Next ›
⏱ 5 min read Modified: 2026-11-29

Java Runtime Images: jlink, Modular JARs and jdeps

jlink is a JDK tool that builds a custom runtime image: a directory with the JVM, the java command and only the modules your application needs. Instead of a full JDK of hundreds of megabytes, you ship a compact runtime with no compiler, no extra modules and nothing to install separately.

jlink --module-path lib --add-modules com.shop.report --output img
img/bin/java --list-modules
com.shop.report@1.0
java.base@25.0.3
java.logging@25.0.3
java.sql@25.0.3
java.transaction.xa@25.0.3
java.xml@25.0.3

Below is the whole path from sources to an image: a modular JAR, dependency analysis with jdeps, the jlink options and typical errors. The lesson builds on Java Modules: module-info, requires, exports and opens. All commands were checked on JDK 25.

1. What a runtime image is and why you need it

Before Java 9 an application ran on a JRE, a ready-made runtime that the user installed separately. Since Java 11 Oracle no longer ships a separate JRE: there is the JDK, and you are expected to build a runtime for your application with jlink. Modules make this possible: the JDK is split into about seventy modules, and jlink takes only the ones you need.

Full JDK 25 jlink image
Modules All platform modules and tools Only the application's modules and their dependencies
javac, jar, jlink Included Not included (unless you add jdk.compiler and other jdk.* modules)
Size (Windows, this lesson's example) about 365 MB about 35–60 MB depending on options
Platform One build per OS The same OS and architecture as the JDK it was built from

Where this helps: Docker images (smaller size means faster pulls and fewer vulnerabilities), desktop applications shipped with their own runtime, and services that do not need the whole JDK.

2. The pipeline: javac, jar, jdeps, jlink

Step Tool Result
1. Compile javac The module's classes and module-info.class
2. Package jar A modular JAR
3. Analyze dependencies jdeps The list of JDK modules the code uses
4. Build the image jlink A directory with the JVM and the required modules

The lesson's example is the shop's reporting module, which needs JDBC:

// src/com.shop.report/module-info.java
module com.shop.report {
    requires java.sql;
    exports com.shop.report;
}

// src/com.shop.report/com/shop/report/ReportApp.java
package com.shop.report;

import java.sql.Connection;

public class ReportApp {
    public static void main(String[] args) {
        System.out.println("module: " + ReportApp.class.getModule().getName());
        System.out.println("uses: " + Connection.class.getName());
        System.out.println("java: " + System.getProperty("java.home"));
    }
}
javac -d mods --module-source-path src -m com.shop.report

3. Modular JARs: creating and running

A modular JAR is an ordinary JAR with module-info.class at its root. JAR files in general are covered in How to Create and Use JAR Files in Java; here is what modules add:

jar --create --file lib/report.jar --main-class com.shop.report.ReportApp --module-version 1.0 -C mods/com.shop.report .

jar --list --file lib/report.jar
META-INF/
META-INF/MANIFEST.MF
module-info.class
com/
com/shop/
com/shop/report/
com/shop/report/ReportApp.class

module-info.class sits at the root of the archive, not in META-INF. The --main-class option (short forms: -e, or the letter e in jar cfe app.jar Class ...) records the main class both in the manifest and in the module descriptor. --module-version sets the module version. Both show up in the description:

jar --describe-module --file lib/report.jar
com.shop.report@1.0 jar:file:///.../lib/report.jar!/module-info.class
exports com.shop.report
requires java.base mandated
requires java.sql
main-class com.shop.report.ReportApp

The same modular JAR can be run in four ways, and the result differs:

Command How it runs getModule().getName()
java -p lib/report.jar -m com.shop.report As a module, main class from the module descriptor com.shop.report
java -p lib -m com.shop.report/com.shop.report.ReportApp As a module, main class given explicitly com.shop.report
java -jar lib/report.jar Class path, main class from the manifest null
java -cp lib/report.jar com.shop.report.ReportApp Class path null

java -jar always runs the JAR from the class path, so module-info.class is ignored and the code lands in the unnamed module. If the JAR was built without --main-class, neither short form works:

java -p nomain.jar -m com.shop.report
module com.shop.report does not have a ModuleMainClass attribute, use -m <module>/<main-class>

java -jar nomain.jar
no main manifest attribute, in nomain.jar

4. jdeps: which modules an application needs

jdeps analyzes bytecode and shows dependencies of modular and plain JAR files and of class directories:

jdeps -s lib/report.jar
com.shop.report -> java.base
com.shop.report -> java.sql

jdeps --print-module-deps lib/report.jar
java.base,java.sql

jdeps --list-deps lib/report.jar
   java.base
   java.sql

--print-module-deps prints a comma-separated list, exactly the format jlink --add-modules expects. Only direct dependencies are shown: java.logging and java.xml, which java.sql requires transitively, are not in the list, but jlink adds them itself.

Two more useful options:

  • --jdk-internals finds uses of JDK internal APIs that may disappear in a new version. For a class that uses sun.misc.Unsafe, the output contains the line u.U2 -> sun.misc.Unsafe JDK internal API (jdk.unsupported) and a table with the suggested replacement.
  • --generate-module-info creates a draft module-info.java for a plain JAR: for legacy-util.jar with package u you get module legacy.util { exports u; }. It is the first step in moving a library to modules.

The minimal command is the module path, the root modules and the output directory:

jlink --module-path lib --add-modules com.shop.report --output img

--add-modules names the root modules. jlink finds everything they require, including transitive dependencies, which is why six modules ended up in the image although only one was given (the list is at the top of the lesson). JDK modules are taken from the installed JDK's jmods directory automatically; you do not need to put it on --module-path.

The image is an ordinary directory:

img/
├── bin/       # java, support libraries
├── conf/      # security and logging settings
├── include/   # header files for JNI
├── legal/     # module licenses
├── lib/       # modules in the lib/modules file and native libraries
└── release    # JAVA_VERSION="25.0.3", MODULES="java.base java.logging ... com.shop.report"

The application is started with the java command from the image itself, with no -p: the module is already inside:

img/bin/java -m com.shop.report
module: com.shop.report
uses: java.sql.Connection
java: ...\img

The java.home property points to the image directory: the program runs on its own runtime, not on the installed JDK.

6. jlink options: --launcher, --strip-debug, --compress, --bind-services

Option What it does
--launcher name=module Creates a launch script in bin; the main class comes from the module descriptor
--launcher name=module/class The same, with the main class given explicitly
--strip-debug Removes debug information from classes
--no-header-files, --no-man-pages Do not copy the include directory and man pages
--compress zip-0 … zip-9 Resource compression: zip-0 is none, zip-9 is the best. The old values 0, 1, 2 are deprecated
--bind-services Adds service provider modules and their dependencies
--add-modules ALL-MODULE-PATH Makes every module on --module-path a root
--list-plugins Lists all plugins and their options

An image "for shipping" is usually built like this:

jlink -p lib --add-modules com.shop.report --launcher report=com.shop.report \
      --strip-debug --no-header-files --no-man-pages --compress zip-9 --output img2

img2/bin/report
module: com.shop.report
uses: java.sql.Connection

The trailing backslash is a bash line continuation; on Windows write the command on one line or use ^ (cmd) or ` (PowerShell). On Windows both report and report.bat are created. In this example the image shrank from about 59 MB to 35 MB. If the module has no main class (for example, a directory produced by javac), the form --launcher name=module fails with doesn't contain main class & main not specified in command line; use name=module/class instead.

--bind-services matters for applications with services. Nobody requires provider modules through requires, so a plain jlink does not see them. The payment-methods example from the services lesson:

jlink -p out --add-modules com.shop.app --output imgs1
imgs1/bin/java -m com.shop.app/com.shop.app.Checkout
                                   # nothing: no providers in the image

jlink -p out --add-modules com.shop.app --bind-services --output imgs2
imgs2/bin/java -m com.shop.app/com.shop.app.Checkout
wallet: paid 100 from wallet
crypto (bitcoin): paid 100 in bitcoin
card: paid 100 by card

But --bind-services has a price: it adds providers of all services, including the JDK's own. In this example the image grew from 3 to 50 modules and from about 49 MB to 162 MB, pulling in, for example, java.desktop and java.compiler. That is why the required providers are more often listed explicitly: --add-modules com.shop.app,com.shop.card. If all providers are on the module path, --add-modules ALL-MODULE-PATH works too: in this example the image had five modules (the four shop modules and java.base), about 49 MB, and the program found all three payment methods.

7. jlink limitations and errors

Situation Result
The --output directory already exists Error: directory already exists: img; the image is not overwritten, delete the directory first
No --add-modules Error: no modules specified to --add-modules
A plain JAR without module-info on --module-path Error: automatic module cannot be used with jlink
You need javac in the image Add the jdk.compiler module to --add-modules
Image built on Windows, run on Linux Does not work: the image contains the native JVM of the platform it was built on

The automatic-module restriction is the main obstacle in practice: if even one library is not modular, you cannot link it directly. The options are to wait for a modular version, create a module-info for it (with jdeps --generate-module-info), or build an image from JDK modules only and run the application from the class path, as in the next section. Automatic modules are covered in more detail in this module's migration lesson.

8. An image for an application without module-info

jlink is useful for an ordinary class-path application too: the image is built from JDK modules only, and the application JAR runs next to it. jdeps suggests the module list:

jdeps --print-module-deps r.jar
java.base,java.sql

jlink --add-modules java.base,java.sql --output imgcp
imgcp/bin/java -jar r.jar
java.sql.Connection

If you forget a module, the error shows up only at run time: in an image with only java.base the same application fails with NoClassDefFoundError: java/sql/Connection. So take the module list from jdeps rather than writing it by hand. Another trap is classes loaded by name through reflection: jdeps does not see them, so add those modules to --add-modules yourself.

Frequently asked questions

How is a jlink image different from a JRE?

A JRE was a universal runtime with all platform modules, the same for every application. A jlink image is built for a specific application and contains only its modules and their dependencies, so it is smaller. Oracle does not ship a separate JRE for Java 11 and later; jlink images play that role.

Can I run javac in a jlink image?

Only if you add the jdk.compiler module when building it: --add-modules com.shop.report,jdk.compiler. By default the image contains just the java command and the modules the application needs; the JDK development tools are not there.

Why does jlink report "automatic module cannot be used with jlink"?

A plain JAR without module-info.class ended up on --module-path, and the JVM turns it into an automatic module. jlink works only with explicit modules. You need a modular version of the library, your own module-info for it, or an image built from JDK modules only with the application run from the class path.

How do I know which modules to pass to --add-modules?

For a modular application, its main module is enough: jlink finds the rest through requires. For a plain JAR, run jdeps --print-module-deps app.jar and pass the result to --add-modules. Add service provider modules and classes loaded by name through reflection yourself, or use --bind-services.

What is the difference between java -jar and java -p app.jar -m module?

java -jar runs the JAR from the class path: module-info.class is ignored, the code runs in the unnamed module, and getModule().getName() returns null. java -p app.jar -m module runs a named module with all JPMS checks: requires, exports and services.

Comments

Please log in or register to have a possibility to add comment.