Sub topic 1
Why build a custom runtime?
Prerequisites
This chapter assumes you can:
- Compile and run a Java program with
javacandjava. - Use a command line to run commands and read their output.
- Explain what a JAR file is and what the classpath does.
You do not need to know the module system yet. The next chapters introduce it. Here we look at the problem that jlink solves, and when the solution is worth its cost.
The problem: shipping Java means shipping a JVM
A Java program needs a Java runtime on the machine where it runs. Your JAR holds bytecode, and the JVM turns that bytecode into running code. That leaves you with a distribution question: where does the runtime come from?
There are three common answers.
- Require the user to install Java. Your download stays small, but you depend on whatever version the user has. You also depend on how that version was installed and kept up to date.
- Ship a full JDK next to your application. You control the version, but you bundle everything the JDK contains. That includes the compiler, debugging tools, and modules your program never loads.
- Build a custom runtime image. You ship only the parts of the Java platform your application uses, plus the application itself.
The third option is what jlink provides.
What is in a full JDK
Since Java 9, the JDK has been split into modules. A module is a named group of packages with declared dependencies. You can see the split on any JDK 9 or later installation:
java --list-modulesThe output is a long list of names such as java.base, java.logging, java.sql, java.xml, and jdk.compiler. Each name is one piece of the platform.
A typical command-line application uses only a small part of this list. A web service might need java.base for core classes, java.logging for logging, and java.net.http for an HTTP client. It will never touch java.desktop (Swing and AWT), java.sql, or jdk.javadoc. A full JDK installs all of them anyway.
You can measure the cost on your own machine. This command prints the total size of the JDK directory:
du -sh "$JAVA_HOME"The result depends on your vendor, version, and platform. It is commonly a few hundred megabytes. A runtime image built from just java.base is much smaller. It is typically tens of megabytes. Measure both on your target platform rather than trusting a general figure.
Where a Java 9+ JRE went
Older tutorials mention the JRE (Java Runtime Environment), a smaller download that had no compiler or development tools. Starting with Java 9, the JDK build no longer produces a separate JRE as a standard part of its design. Several vendors still publish JRE-style packages for some versions, but you cannot rely on every vendor and version offering one.
Even where a JRE package exists, it is a fixed bundle. It includes the modules the vendor chose, not the modules you need. A custom runtime image is the module-aware replacement: you choose the contents.
What jlink does
jlink is a tool in the JDK's bin directory. It reads a list of root modules, follows their dependencies, and writes a directory containing a runtime with only those modules. That directory is self-contained. It has its own java launcher, its own class library, and your application's modules.
The process has three inputs and one output:
- Root modules that you name, usually your application module.
- A module path that tells
jlinkwhere to find the modules, including the JDK's own. - Options that control launchers, compression, and what to strip.
- An output directory that becomes the runtime image.
The following diagram shows the flow.
Later chapters cover each step. For now, this is the shape of a typical command, shown only to make the idea concrete:
jlink \
--module-path "$JAVA_HOME/jmods:mods" \
--add-modules com.example.app \
--output build/runtimeRead it from the top. --module-path says where modules live. --add-modules names the root module. --output names the directory to create. The tool works out the rest from the dependencies the modules declare.
Benefits
Smaller distributions
The most visible gain is size. A smaller image means a smaller download, a faster upload to a registry, and less disk use on each machine. In a container, the runtime is often the largest layer, so trimming it has a direct effect on image size. Options such as --strip-debug, --no-header-files, and --no-man-pages reduce it further, as a later chapter shows.
A known, fixed runtime
When you ship the runtime, you decide which Java version runs your code. The application no longer breaks because a user has an older Java, or a newer one with different defaults. You can test against the exact runtime you deliver.
No separate install step
Users do not have to install Java first. They unpack your directory and run its launcher. This helps with desktop tools, command-line utilities, and appliances where you do not control the machine.
A smaller attack surface
Modules you do not ship cannot be loaded and cannot contain vulnerabilities that affect you. This is a modest benefit, not a security strategy. It does reduce the amount of code you need to track in security advisories.
Costs
A custom runtime is not free. You should know the costs before choosing it.
- You own the update. When the vendor releases a security patch, users of a shared Java install get it through their normal update path. With a bundled runtime, you must rebuild and redistribute your image.
- Images are platform-specific. An image holds native code. One built on Linux x64 will not run on Windows or macOS. You need one image per target platform.
- Not every project fits.
jlinkworks best with modular code. Applications that use only classpath JARs need extra steps, covered in a later chapter. - Dynamic features can be missed. If code loads a class by name at run time, the tools that compute dependencies may not see it. The image then lacks a module that the program needs.
The last point is the most common error. The image builds without complaint, and the failure appears only when the program runs. A later chapter on verification shows how to catch it.
Choosing between the options
| Situation | Reasonable choice |
|---|---|
| Internal service, managed servers with a standard Java install | Rely on the installed Java |
| Container image where you control the base image | Custom runtime, to reduce layer size |
| Desktop or command-line tool sent to unknown machines | Custom runtime, so no install is needed |
| Application that needs many JDK modules anyway | Full JDK, or a custom runtime with little saving |
| Fast-moving team with no process for rebuilding on patches | Shared install, or add a rebuild process first |
The pattern is simple. The more you control the target machine, the less a bundled runtime helps. The less you control it, the more it helps.
What comes next
The next chapters build the knowledge jlink depends on. You will learn what modules are, how a module declares its dependencies, and how to find the JDK modules your application uses. Then you will build, inspect, and trim an image.
Exercise
Your team ships a command-line tool to customers who may have no Java installed or may have an old version. The tool needs only core classes and logging. Name two benefits of giving them a jlink runtime image instead of asking them to install a JDK, and one cost you accept.
Show answer
Benefits: the tool runs on a known Java version that you tested, and customers need no separate install, so the download is also much smaller than a full JDK. Cost: you must rebuild and redistribute the image whenever the JDK vendor releases a security patch, and you need a separate image for each operating system and architecture you support.
Exercise
Run java --list-modules and du -sh "$JAVA_HOME" on your machine. List three modules that you think a simple command-line tool would never need, and say why.
Show answer
Answers vary by JDK. Reasonable choices include java.desktop (it holds the Swing and AWT graphical toolkits), jdk.javadoc (it generates API documentation), and jdk.compiler (it compiles source code, which a finished tool does not do). A tool that does not use a graphical interface, document generation, or compilation can leave these out.