I created runtime image using jlink on my Linux machine. And I see linux
folder under the include
folder. Does it mean that I can use this runtime image only for Linux platform? If yes, are there any ways to create runtime images on one platform for another (e.g. on Linux for Windows and vice versa)

- 2,267
- 2
- 18
- 23
2 Answers
The include
directory is for header files, such as jni.h
, that are needed when compiling C/C++ code that uses JNI and other native interfaces. It's nothing to do with jlink
.
The jlink
tool can create a run-time image for another platform (cross targeting). You need to download two JDKs to do this. One for the platform where you run jlink
, the other for the target platform. Run jlink
with --module-path $TARGET/jmods
where $TARGET
is the directory where you've unzipped the JDK for the target platform.

- 5,283
- 1
- 20
- 25
Being generally unable to add anything to Alan Bateman's answers in terms of information, I'll offer a working example. This example illustrates using jlink
on Mac OS and then running the binary on Ubuntu in a Docker container.
The salient points are as follows.
Given two simple modules, we compile on Mac OS:
javac -d build/modules \
--module-source-path src \
`find src -name "*.java"`
jar --create --file=lib/net.codetojoy.db@1.0.jar \
-C build/modules/net.codetojoy.db .
jar --create --file=lib/net.codetojoy.service@1.0.jar \
-C build/modules/net.codetojoy.service .
Assuming that the Linux 64 JDK is unpacked in a local directory (specified as command-line arg), we call jlink
(on Mac OS in this example). JAVA_HOME
is the crux of the solution:
# $1 is ./jdk9_linux_64/jdk-9.0.1
JAVA_HOME=$1
rm -rf serviceapp
jlink --module-path $JAVA_HOME/jmods:build/modules \
--add-modules net.codetojoy.service \
--output serviceapp
Then, assuming we've pulled the ubuntu
image for Docker, we can execute the following in a Docker terminal (i.e. Linux):
docker run --rm -v $(pwd):/data ubuntu /data/serviceapp/bin/java net.codetojoy.service.impl.UserServiceImpl
TRACER : hello from UserServiceImpl
To re-iterate this feature of Java 9/jlink
: Linux does not have Java installed and the Linux binary was built on Mac OS.

- 23,733
- 7
- 76
- 107
-
7Using JAVA_HOME as the variable name is unnecessarily confusing. Call it LINUX_JAVA_HOME or UNPACKED_LINUX_JAVA or WHATEVER. – Perdi Estaquel Jan 17 '19 at 03:02
-
2What is the functional purpose of the docker for the average reader? Why not just extract the target JDK to a local directory? Also, please fix the improper use of the reserved `$JAVA_HOME` variable per comment above. – tresf Apr 16 '19 at 15:44
-
1Using docker is overcomplicating things unnecessarily here. – Guntram Blohm May 14 '19 at 13:24
-
5On the other hand, using docker here shows you how to test your newly created binary on the same machine without using a VM, rebooting or anything else. – Dmitry Avtonomov Jan 29 '20 at 19:30