Sub topic 1
Why build a custom runtime?
Prerequisites
This chapter assumes you can compile and run Java programs with javac and java. It also assumes you are comfortable at the command line and know what a JAR file and the classpath are. You do not need to know the module system yet. Later chapters cover it.
The problem with shipping a full JDK
A Java application needs a Java runtime to run. The usual ways to supply one are:
- Ask users to install a JDK or JRE themselves.
- Bundle a complete JDK with the application.
- Build a runtime that contains only what the application uses.
The first option makes your application depend on whatever the user has installed. The version may be wrong, or missing entirely. The second option fixes that, but a full JDK is large. A typical JDK installation takes several hundred megabytes. Most applications use a small part of it. A command-line tool might never touch the desktop, XML, or database modules that ship with the JDK.
The third option is what jlink provides. jlink is a tool included in the JDK since Java 9. It assembles a runtime image: a directory with a Java runtime and the application's modules, containing only the JDK modules you ask for and the modules they depend on.
What a custom runtime gives you
- Smaller size. You ship only the modules your application needs. This matters for container images, installers, and downloads.
- Predictable behavior. The application runs on the exact Java version you tested with. It does not depend on what is installed on the target machine.
- Easier distribution. The image is a self-contained directory. The user does not install Java first.
- Smaller attack surface. Modules that are not in the image cannot be used or attacked. You also have fewer components to patch.
How the pieces fit together
jlink relies on the Java module system. Your application declares which modules it requires. The JDK itself is split into modules such as java.base, java.logging, and java.sql. jlink reads the dependencies, resolves the full set of modules needed, and copies only those into the image.
The rest of this tutorial follows that flow:
- Learn the module basics and how a module declares its dependencies.
- Use
jdepsto find which JDK modules your application needs. - Run
jlinkto build the image. - Trim the image, add a launcher, and check that it works.
- Handle harder cases such as services, non-modular code, other platforms, containers, and CI.
A first look at the result
You will build this example in later chapters. For now, the commands below show the end goal. They list the modules in a finished image and run the application through a generated launcher.
build/runtime/bin/java --list-modules
build/runtime/bin/helloThe first command prints only a handful of modules, such as java.base, java.logging, and your own com.example.app. A full JDK lists around seventy.
When a custom runtime is not the right choice
A custom runtime has costs. You become responsible for updating the bundled Java version. When a security fix is released, you must rebuild and redistribute the image. Applications that rely heavily on non-modular libraries need extra work. If many applications on one machine can share a single installed JDK, separate images use more disk space in total. Later chapters return to these trade-offs.
Exercise
Name two benefits of shipping an application with a jlink runtime image instead of a full JDK, and one cost you take on.
Show answer
Benefits: a smaller download or container image, because only needed modules are included; and predictable behavior, because the application runs on the Java version you tested with. Other valid benefits are easier distribution and a smaller attack surface. Cost: you must rebuild and redistribute the image whenever the bundled Java version needs a security or bug-fix update.
Exercise
Why must you build a separate runtime image for each target operating system?
Show answer
The image contains platform-specific parts of the JDK, such as native libraries and launchers. An image built from a Linux JDK will not run on Windows or macOS.