Sub topic 14
Targeting another operating system
This chapter assumes you can build a basic image with jlink, as in the earlier chapters. It also assumes you know what --module-path and --add-modules do.
The image is tied to a platform
A runtime image contains native code: the JVM, native libraries, and launchers. An image built from a Linux JDK will not run on Windows or macOS. An image for x64 will not run on AArch64.
The Java bytecode in your application modules is platform-independent. The native parts come from the JDK modules, so the choice of platform comes down to which JDK modules you give jlink.
How jlink picks the target
jlink takes the platform from the modules on --module-path, not from the JDK that runs the tool. To build for another platform:
- Download a JDK for the target platform. Use the same version as the JDK that runs
jlink. - Unpack it somewhere. You don't need to install it.
- Point
--module-pathat that JDK'sjmodsdirectory instead of$JAVA_HOME/jmods.
Example: building a Windows image on Linux
Suppose you unpacked a Windows x64 JDK into ~/jdks/jdk-windows. The command is the same as before, with a different jmods path:
jlink \
--module-path "$HOME/jdks/jdk-windows/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-windowsThe path separator in --module-path belongs to the host running jlink, not to the target. On a Windows host, use a semicolon.
The output has the Windows layout. The launcher is bin/hello.bat instead of the shell script bin/hello, and the native libraries are .dll files.
Checking the result
You can't run the image on the build machine, so inspect it instead. The release file records the platform:
cat build/runtime-windows/releaseLook for entries such as OS_NAME and OS_ARCH. Then copy the image to a real Windows machine, or a CI runner for that platform, and run java --list-modules and the launcher there.
Common errors
- Version mismatch. If the
jlinkversion and the targetjmodsversion differ, the build may fail or produce an image that doesn't work. Match the feature release exactly, such as 21 with 21. - Platform-specific dependencies. If your application depends on a JAR with native libraries (for example JNI code), that JAR must supply the libraries for the target platform.
jlinkdoes not translate them. - Mixed
jmods. Never putjmodsdirectories from two platforms on the same module path.
Exercise
You run jlink from a Linux JDK 21 with --module-path pointing at the jmods directory of a macOS AArch64 JDK 21. What platform is the image for, and why can't you run it on your Linux machine?
Show answer
The image is for macOS on AArch64, because jlink takes the platform from the jmods on the module path. It contains macOS native code (the JVM and libraries), which the Linux machine cannot execute. You must copy it to a macOS AArch64 machine to run and verify it.