When distributing a standalone Java application, if you include the JRE, you do not need to set the environment. For that purpose, you need to include the JRE for each platform in the application. You can bundle a full-size JRE, but since Java 9 and later modules are supported, it is desirable to create a JRE that contains only the necessary amount, but in that case each You will need the files under jmods included in the JDK for your platform.
It's surprisingly troublesome to manually download the JDK for setup, so I added the following script to build.gradle.
plugins {
id "de.undercouch.download" version "4.0.4"
}
task prepareJDK{
downloadAndExtractJDK("jdk/win",'http://download.bell-sw.com/java/11.0.6+10/bellsoft-jdk11.0.6+10-windows-amd64-full.zip')
downloadAndExtractJDK("jdk/mac",'http://download.bell-sw.com/java/11.0.6+10/bellsoft-jdk11.0.6+10-macos-amd64-full.zip')
}
void downloadAndExtractJDK(jdkDir,url){
def zipFile = "${jdkDir}/jdk.zip"
if(file(zipFile).exists()==false){
download {
src url
dest zipFile
}
}
if(file("${jdkDir}/LICENSE").exists()==false){
copy{
from( zipTree(zipFile) ){
include "**"
eachFile { fcd ->
fcd.relativePath = new RelativePath(true, fcd.relativePath.segments.drop(1))
}
includeEmptyDirs = false
}
into jdkDir
}
}
}
task buildJRE(type: Exec,dependsOn:prepareJDK) {
commandLine './buildJRE.sh'
}
this
gradle buildJRE
Then the JDK will be downloaded and a JRE for each platform will be generated in bin / jre.
Download and unpack Liberica JDK 11 for Win / Mac in the prepareJDK task. The task itself is simple, but when unzipping it uses RelativePath to pull up the one-tier file path. If you unzip the JDK normally, a folder containing the JDK version will be created in the form of "jdk / win / jdk-11.0.6", but when you create the JRE, it will be used a little if the version is entered. Since it was difficult, the files in the folder are pulled up one level.
The shell that generates the JRE (buildJRE.sh) is the shell that calls jlink.
#!/usr/bin/env sh
MODULES="java.base,java.desktop,javafx.controls"
for PLATFORM in win mac
do
OUTPUT_DIR=build/jre/$PLATFORM
JMODS_DIR=jdk/$PLATFORM/jmods
if [ -d $OUTPUT_DIR ]; then
echo "JRE for $PLATFORM exits in $OUTPUT_DIR"
else
echo "creating JRE for $PLATFORM in $OUTPUT_DIR"
jlink --compress=2 --module-path $JMODS_DIR --add-modules $MODULES --output $OUTPUT_DIR
fi
done
In MODULES, write down the modules to be used. The Librica JDK comes with JavaFX, which is very useful for JavaFX apps.
I am very pleased that the development has moved to OpenJDK in earnest, various JDKs can be downloaded, and only the necessary modules can be created.
Recommended Posts