jlink Example: Create a Custom Minimal JRE in Java 9
-
Last Updated:
-
By: Suraj S R
-
Series

A beginner-friendly jlink example for Java 9. Build a custom minimal JRE step by step with jdeps and jlink, shrink it, and run your app anywhere.
If you have ever shipped a Java app, you know the pain. The app itself is small, but the Java runtime you send along is huge. This jlink example will show you a simple way to fix that. With jlink, a tool added in Java 9, you can build your own tiny JRE that holds only the modules your app actually needs. So instead of shipping a few hundred megabytes, you ship something much lighter.
In this guide, we will start from zero. First, we will see what jlink is and why it exists. Then we will build a small modular app and turn it into a custom runtime, step by step. By the end, you will be able to run your program without any full Java installation on the machine. The language here is kept very simple, so beginners can follow along without getting lost.
A small note before we dive in. You do not need to be a module expert to follow this. We will touch only the module bits that jlink needs, and we will explain each one as it shows up. So treat this as a friendly, hands-on walk rather than a dry reference.
jlink is a command line tool that comes inside the JDK. You will find it in the bin folder, right next to java and javac. Its job is easy to describe. It takes a set of modules, pulls in the other modules they depend on, and packs them into one custom runtime image. In plain words, it builds a small, ready-to-run Java runtime made just for your app.
This tool arrived in Java 9 as part of JEP 282, also called “jlink: The Java Linker”. It works hand in hand with the Java Platform Module System (JPMS), which also landed in Java 9. Because modules describe their own dependencies, jlink can look at them and figure out exactly what to include. Nothing more, nothing less.
A full JDK is large. It can take several hundred megabytes on disk. Most apps, however, use only a small slice of it. Your simple service may never touch sound, printing, or desktop UI modules. Yet, with the old approach, all of that still came along for the ride.
A trimmed runtime brings real, everyday benefits:
Before Java 9, the build team made JRE and JDK images by copying the whole runtime and then deleting unused parts. That felt clumsy. jlink flips the idea around. It adds only what you ask for, so you build up instead of cutting down.
This shift matters more than it first looks. Since you start from nothing, you stay in full control of the final size. You decide which modules go in, and jlink respects that choice. As a result, two apps can get two very different runtimes, each shaped to its own needs.
There is one more neat idea here. JEP 261 introduced the notion of “link time”. Think of it as a new optional step that sits between compile time and run time. At link time, jlink can also run small optimizations that are hard to do earlier or costly to do later. That is why the tool is called a linker.
Let us keep the mental model clear before we touch the keyboard. jlink needs three things from you, and then it hands back one thing.
You give it:
In return, jlink gives you a self-contained runtime image. It has the familiar layout you already know, with a bin folder, a conf folder, and a lib folder. Inside bin sits a java launcher that runs on that trimmed runtime. As a result, you can zip the folder, copy it to another machine, and run your app there.

One important caveat lives here. The image jlink builds is platform specific. It carries native bits from the JDK you used. So an image built on Windows runs on Windows, and a Linux image runs on Linux. To target another operating system, run jlink using a JDK for that system.
You do not need much for this jlink example. Still, a quick check saves time later. Make sure you have the following ready on your machine.
To confirm your setup, open a terminal and check the version. If the command below prints 9 or higher, you are good to go.
java -version # example output # openjdk version "17.0.10" ... jlink --version # prints the jlink/JDK version, e.g. 17.0.10
If jlink is not found, your PATH is probably missing the JDK bin folder. Fix that first, because jlink lives only inside a JDK, not inside a plain JRE.
Now comes the fun part. We will build a tiny modular app and turn it into its own runtime. The app just logs a message, so it needs the logging module on top of the base module. We will go slowly, one step at a time.
First, make a project folder. Inside it, create a src folder. Then add a module-info.java file and one small class. The module-info file is the heart of a module. It names the module and lists what it requires.
jlink-demo/
src/
module-info.java
com/javahandson/demo/HelloJlink.javaHere is the module descriptor. Our module is called com.javahandson.demo, and it needs the java.logging module.
// src/module-info.java
module com.javahandson.demo {
requires java.logging;
}Next, write the main class. It uses a Logger, so the logging requirement above makes sense.
// src/com/javahandson/demo/HelloJlink.java
package com.javahandson.demo;
import java.util.logging.Logger;
public class HelloJlink {
private static final Logger LOG =
Logger.getLogger(HelloJlink.class.getName());
public static void main(String[] args) {
LOG.info("Hello from a custom jlink runtime!");
}
}Now compile the code into an output folder named out. The compiler reads your source and writes the module there. This step is plain javac, nothing fancy.
javac -d out $(find src -name "*.java") # On Windows (PowerShell), list the files directly: # javac -d out src/module-info.java src/com/javahandson/demo/HelloJlink.java
After this runs, you will see an out folder. It holds the compiled module, ready to run and ready to link.
Before you link, you should know which modules your app truly needs. The jdeps tool answers this for you. It scans your code and prints the module dependencies. Therefore, you never have to guess.
jdeps --module-path out -s --module com.javahandson.demo # Typical output: # com.javahandson.demo -> java.base # com.javahandson.demo -> java.logging
As you can see, our app needs only java.base and java.logging. The java.base module is always required, because it holds core classes like String, Object, and System. Good. Now we know exactly what to pack.
Here is the main jlink command. It points at two places on the module path: the JDK jmods folder for platform modules, and our out folder for the app module. Then it adds our module and writes the result to a folder named myruntime.
jlink \ --module-path "$JAVA_HOME/jmods:out" \ --add-modules com.javahandson.demo \ --output myruntime # Windows uses a semicolon as the path separator: # jlink --module-path "%JAVA_HOME%\jmods;out" ^ # --add-modules com.javahandson.demo --output myruntime
When it finishes, look inside the myruntime folder. You will find the standard runtime layout. The bin folder has a java launcher, the lib folder holds the packed modules, and conf holds configuration. In short, this is a real, working mini-JRE built just for your app.
Finally, run the program using the java binary inside your new image. Do not use your system java here. Instead, call the one inside myruntime, so you prove the custom runtime really works.
./myruntime/bin/java --module com.javahandson.demo/com.javahandson.demo.HelloJlink # On Windows: # myruntime\bin\java --module com.javahandson.demo/com.javahandson.demo.HelloJlink # Output: # INFO: Hello from a custom jlink runtime!
That is it. You just built and ran your first custom runtime. Moreover, you can now copy the myruntime folder to another machine of the same OS and run it there, with no Java install needed.
It helps to peek inside the folder jlink just created. The layout looks a lot like a normal JRE, only much smaller. Once you know the parts, the whole thing feels less like magic and more like a simple file tree.
Here are the main folders and files you will see:
You can also ask the runtime to list its own modules. This is a quick way to confirm that only the modules you expected made it in. Run the command below against your new image.
./myruntime/bin/java --list-modules # Expected output for our demo (the version matches your JDK): # com.javahandson.demo # java.base@17.0.10 # java.logging@17.0.10
Notice the short list. There is no desktop module, no sound module, and no database module. Only the three modules our app truly needs are present. That small list is exactly why the image stays so light.
The three options above do the heavy lifting. Let us look at each one, because understanding them makes every later jlink example easier to read.
This option tells jlink where to discover modules. You usually point it at two spots. One is the JDK jmods folder, which holds the platform modules. The other is your own build folder, which holds your app modules. Separate multiple entries with a colon on Linux and macOS, or a semicolon on Windows.
This option lists the root modules you want in the image. jlink starts from this set and then pulls in every module they depend on. The default set is empty, so you must name at least one module here. Usually, you name your app module, and jlink handles the rest.
This one is simple. It names the folder where jlink writes the final runtime image. The folder must not exist yet, or jlink will stop and complain. So pick a fresh name each time, or delete the old folder first.
A few more options show up often. They help you shrink the image or make it easier to launch. You do not need all of them at once, but they are handy to keep in mind.
The basic image already saves a lot. Even so, you can squeeze it further. The trick is to combine a few size options in one command. Each one shaves off a little, and together they add up.
jlink \ --module-path "$JAVA_HOME/jmods:out" \ --add-modules com.javahandson.demo \ --strip-debug \ --no-header-files \ --no-man-pages \ --compress=2 \ --output myruntime-small
Let us break down what each flag buys you:
One gentle warning here. Compression trades a little startup speed for a smaller size. For most apps, the difference is tiny. Still, if startup time is critical for you, measure both ways and then decide.
Typing the full module and class name every time gets old fast. Luckily, jlink can create a short launcher script for you. You pass –launcher with a name, the module, and the main class. After that, you run your app with a single short command.
jlink \ --module-path "$JAVA_HOME/jmods:out" \ --add-modules com.javahandson.demo \ --launcher hello=com.javahandson.demo/com.javahandson.demo.HelloJlink \ --output myruntime-launcher # Now run it with one short command: ./myruntime-launcher/bin/hello
Behind the scenes, jlink writes a small script named hello into the bin folder. That script simply starts the trimmed runtime and points it at your main class. Consequently, running your app becomes as easy as calling one name.
| 💡 Interview Insight A very common question is: how is jlink different from jpackage and jdeps? Keep the answer short and clear. jdeps analyzes your code and tells you which modules it depends on. jlink takes those modules and builds a custom, minimal runtime image. jpackage goes one step further and wraps your app plus a jlink runtime into a native installer, like an MSI on Windows or a DEB on Linux. In short: jdeps analyzes, jlink links, and jpackage packages. Also remember that jlink works with modules and that its image is platform specific — two points interviewers love to probe. |
You may wonder about older apps that have no module-info.java. Here is the honest truth. jlink is built around the module system, so it expects modules. A plain JAR with no module descriptor is treated as an automatic module, and jlink cannot link automatic modules directly.
So what can you do? You have two practical paths:
For a quick runtime that runs classpath code, the first path works well. You simply pick the platform modules your app touches and build an image from them.
# A minimal runtime with just the base module jlink --add-modules java.base --output base-runtime # Check what is inside ./base-runtime/bin/java --list-modules
Beginners hit the same few snags with jlink. Knowing them upfront saves a lot of head-scratching. Here are the usual ones, with simple fixes.
jlink refuses to write into a folder that is already there. The fix is easy. Either delete the old folder or choose a new output name. This keeps builds clean and avoids mixing old files with new ones.
If jlink says it cannot find a module, your module path is likely wrong. Double check two things. First, confirm the JDK jmods folder is on the path. Second, confirm your own build folder is there too. A single missing entry causes this error.
Remember, the image is platform specific. An image built on Linux will not run on Windows. To support both, build one image per operating system, each with the matching JDK. Many teams do this inside their CI pipeline.
A custom runtime does not update itself. The Java auto-update that patches a normal JRE does not touch your image. Therefore, you own the updates. When a new JDK security release lands, rebuild your image and ship it again.
jlink is powerful, yet it is not for every single project. It shines in some cases and adds little in others. So let us look at where it really pays off.
Great fits for a custom runtime:
This is not a toy idea, by the way. Minecraft: Java Edition ships with a custom embedded runtime. Large companies also deploy many JVMs this way to keep things lean. If it scales for them, it will serve your projects well too.
On the other hand, a quick local script or a throwaway test does not need jlink. In those cases, your normal JDK is simpler. Use the tool where the size and deployment wins actually matter.
A good rule of thumb helps here. Ask yourself one question. Will this app be shipped, packed, or deployed somewhere else? If yes, a custom runtime usually pays off. If it only runs on your own machine, you can safely skip it.
Docker is where jlink really earns its keep. The idea is a multi-stage build. In the first stage, you use a full JDK and run jlink to make the trimmed runtime. In the second stage, you copy only that runtime into a small base image. So the final image never carries the full JDK.
# ---- Stage 1: build the custom runtime ----
FROM eclipse-temurin:17-jdk AS builder
WORKDIR /app
COPY out ./out
RUN jlink \
--module-path "$JAVA_HOME/jmods:out" \
--add-modules com.javahandson.demo \
--strip-debug --no-header-files --no-man-pages \
--output /javaruntime
# ---- Stage 2: tiny final image ----
FROM debian:stable-slim
COPY --from=builder /javaruntime /javaruntime
ENV PATH="/javaruntime/bin:${PATH}"
CMD ["java", "--module", \
"com.javahandson.demo/com.javahandson.demo.HelloJlink"]The result is a lean container that starts fast and pulls quickly. Because the runtime is small, your deploys stay cheap and simple. This pattern is popular in microservices, where many small services run side by side.
These three tools often appear together, which confuses newcomers. Here is a clean, one-line view of each. Keep it handy, since it also helps in interviews.
Notice how they form a pipeline. You analyze with jdeps, link with jlink, and package with jpackage. Each tool does one job well, and together they cover the whole deployment story.
A: jlink is a command line tool added in Java 9 (JEP 282). It links a set of modules and their transitive dependencies into a custom, minimal runtime image. The result is a small, self-contained JRE made just for your application.
A: Compile your module, run jdeps to find the modules it needs, then run jlink with three options: –module-path (where to find modules), –add-modules (your app module), and –output (the folder for the image). jlink then builds a working mini-JRE in that folder.
A: jlink builds a minimal runtime image from modules. jpackage goes one step further and wraps your app plus a jlink runtime into a native installer, such as an MSI on Windows or a DEB on Linux. In short, jlink links the runtime and jpackage packages the installer.
A: Not directly. jlink is built around the module system and cannot link automatic modules (plain JARs without module-info.java). You can still build a java.base-only image and run classpath code on it, or add a proper module-info.java so jlink can link your app cleanly.
A: No. The image is platform specific because it carries native bits from the JDK you used. An image built on Windows runs on Windows, and a Linux image runs on Linux. To target another operating system, run jlink using a JDK for that system.
A: A java.base-only image is often around 30 to 45 MB, and Microsoft measured about 24 MB on Windows for such an image. Options like –strip-debug, –no-header-files, –no-man-pages, and –compress shrink it further.
Let us wrap up what we covered. You now know the core idea and the main steps. More importantly, you built a working runtime with your own hands.
The best way to lock this in is to try it yourself. Build the demo above, then point jlink at one of your own small modules. Once you see your app run on a trimmed runtime, the whole module system starts to click.