Sub topic 14
Targeting another operating system
This chapter assumes you can build a basic jlink image, read a module-info.java, and run jdeps. It covers how to produce a runtime image for an operating system or CPU architecture other than the one you build on.
Why an image is tied to a platform
A jlink image contains native code. The java launcher, the JVM library (libjvm.so, jvm.dll, or libjvm.dylib) and the native libraries behind modules such as java.base are compiled for one operating system and one CPU architecture. An image built on Linux x64 does not run on Windows or on macOS. It also does not run on Linux AArch64.
Your application's own classes are different. Compiled bytecode is portable. Only the JDK part of the image is platform specific.
So the image has to be assembled from JDK files that match the target. The jlink you run can be from the build machine. The JDK modules it copies must be for the target.
How jlink picks the platform
The platform of the image is decided by the JDK modules on the module path, not by the machine running jlink. When you build normally, --module-path points to $JAVA_HOME/jmods, which holds the JMOD files of the JDK that runs jlink. The image therefore matches that JDK.
To target another platform, give jlink the jmods directory of a JDK built for the target:
- Download a JDK for the target platform, for example Windows x64.
- Unpack it somewhere on the build machine.
- Point
--module-pathat that JDK'sjmodsdirectory, followed by your application modules. - Run jlink from the build machine's own JDK.
The two JDKs must be the same feature release. If jlink runs on JDK 21, the target JDK also has to be 21. jlink checks this and refuses to link modules from a different version. Using the same update release is a good habit, because the image should contain the exact JDK build you tested.
A worked example: Linux host, Windows target
The example reuses the com.example.app module from earlier chapters. The build machine is Linux x64 and the target is Windows x64. The Windows JDK was unpacked to /opt/jdk-win.
jlink \
--module-path "/opt/jdk-win/jmods:mods" \
--add-modules com.example.app \
--launcher hello=com.example.app/com.example.app.Main \
--strip-debug --no-header-files --no-man-pages \
--output build/runtime-winSeparate the path entries with a colon here because jlink runs on Linux. The separator depends on the machine running jlink, not on the target. If you run jlink on Windows to build for Linux, use a semicolon.
The result in build/runtime-win is a Windows image. It contains bin/java.exe, bin/hello.bat, and bin/server/jvm.dll. You cannot execute it on the Linux build machine, so you cannot run build/runtime-win/bin/java --list-modules there.
Checking which platform an image targets
Every image has a release file at its root. It records the target. Read it with any tool, since it is plain text:
cat build/runtime-win/releaseLook for these lines:
OS_NAME="Windows"
OS_ARCH="amd64"
JAVA_VERSION="21.0.5"
MODULES="com.example.app java.base java.logging"OS_NAME and OS_ARCH come from the JDK whose modules were linked. If they show the build machine instead of the target, the module path still points at the host JDK.
You can also check the launcher files:
| Target | Launcher files in `bin` | JVM library |
|---|---|---|
| Linux | `java`, `hello` (shell script) | `lib/server/libjvm.so` |
| macOS | `java`, `hello` (shell script) | `lib/server/libjvm.dylib` |
| Windows | `java.exe`, `hello.bat` | `bin/server/jvm.dll` |
Launcher scripts are generated for the target. This is why the platform of the JDK modules matters: jlink writes hello.bat when the linked java.base says the target is Windows.
Scripting several targets
Automating builds for several platforms means one loop over target JDKs. The script below assumes each target JDK is unpacked under /opt/jdks/<name>.
#!/usr/bin/env bash
set -euo pipefail
for target in linux-x64 linux-aarch64 windows-x64 macos-aarch64; do
jlink \
--module-path "/opt/jdks/${target}/jmods:mods" \
--add-modules com.example.app \
--launcher hello=com.example.app/com.example.app.Main \
--strip-debug --no-header-files --no-man-pages \
--output "build/runtime-${target}"
doneEach output directory can then be archived and published separately. Use zip for Windows and tar.gz for Unix-like targets. A tar archive keeps the executable permission bits of bin/java and the launcher. A zip made on a system that drops those bits can leave a Unix image unable to start.
What cross-linking does not cover
Several things can still fail, and none of them show up at link time.
- Native libraries in your dependencies. A third-party JAR may bundle
.so,.dllor.dylibfiles. jlink copies the JAR content but cannot select the right native file for the target. Check the dependency's documentation for per-platform artifacts. - Platform-specific JDK modules. Some modules exist only on certain systems, for example
jdk.accessibilityon Windows. If your--add-moduleslist orjdepsoutput was produced on one OS, check that every module exists in the target JDK'sjmods. jlink reports a missing module as an error. - Output of
jdepson a different JDK. Runjdepswith the JDK you build with, but confirm that the target JDK has all those modules. Differences between vendors' builds are rare, though they exist. - Testing. The image is not run during the build. A cross-built image is untested until it runs on the target, so add a smoke test on a machine or CI runner of that platform.
Cross-linking or native builders
There are two ways to get images for several platforms.
| Approach | Strengths | Weaknesses |
|---|---|---|
| Cross-link on one machine | One build job. Simple to script. Fast. | No test run on the target. Needs the target JDK downloaded. Native dependencies need separate handling. |
| Build on each platform | Image is tested where it runs. No extra JDK downloads. | Needs a runner per OS and architecture. More CI configuration. |
A common compromise is to cross-link in one job, then run a short smoke test on a runner for each platform using the produced image. That keeps the build simple and still catches a broken launcher.
Summary of rules
- The JDK modules on
--module-pathdecide the platform of the image. - The jlink tool and the target JDK must be the same feature release.
- The path separator follows the machine that runs jlink.
- Read the
releasefile to confirmOS_NAMEandOS_ARCH. - Test the image on the target platform before you ship it.
Exercise
You run jlink on a Linux x64 machine with --module-path "$JAVA_HOME/jmods:mods" and give the resulting image to a colleague who uses Windows. The image does not start. What went wrong, and how do you fix the build?
Show answer
$JAVA_HOME/jmods belongs to the Linux JDK, so the image contains Linux native code and a Linux launcher. Download a Windows x64 JDK of the same feature release as the jlink you run, unpack it, and replace $JAVA_HOME/jmods in --module-path with that JDK's jmods directory. Then check that release shows OS_NAME="Windows" and that bin contains java.exe and hello.bat.
Exercise
A cross-linked image for macOS AArch64 builds without errors, and its release file shows the right OS_NAME and OS_ARCH. Name two problems that could still prevent it from working on a Mac.
Show answer
Any two of these: a third-party dependency that bundles native libraries only for other platforms; lost executable permission bits on bin/java or the launcher because of how the image was archived; a problem that only appears at run time because the image was never executed on macOS during the build.