Create a Java runtime image on one platform for another using Jlink

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

+7
java java-9 jigsaw jlink java-module
source share
2 answers

The include directory is for header files, such as jni.h , which are needed when compiling C / C ++ code that uses JNI and other native interfaces. This is not related to jlink .

The jlink tool can create a runtime image for another platform (cross-targeting). For this you need to download two JDKs. One for the platform on which you are running jlink , and the other for the target platform. Run jlink with --module-path $TARGET/jmods , where $TARGET is the directory where you unpacked the JDK for the target platform.

+10
source share

Being generally unable to add anything to Alan Bateman's answers in terms of information, I will give a working example. This example illustrates the use of jlink on Mac OS and then running the binary on Ubuntu in a Docker container.

The main 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 the Linux 64 JDK is unpacked in a local directory (specified on the arg command line), we call jlink (on Mac OS in this example). JAVA_HOME is the key 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 pulled out an ubuntu image for Docker, we can do the following in a Docker terminal (like 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, Java 9 / jlink : Linux is not installed on Java, and the Linux binard was created on Mac OS.

+1
source share

All Articles