[JAVA] Download JDK with Gradle and make JRE

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

Download JDK with Gradle and make JRE
How to download Oracle JDK 8 rpm with curl
Make SpringBoot1.5 + Gradle4.4 + Java8 + Docker environment compatible with Java11
Make HTML page dynamic with thymeleaf (spring + gradle)
Build and test Java + Gradle applications with Wercker
[Review] Reading and writing files with java (JDK6)
Arbitrate the fight between PowerMock and jacoco with Gradle
How to install Gradle and Kotlin with SDKMAN (Mac)
Use ProGuard with Gradle
Integration Test with Gradle
Download and launch Zipkin
Install Gradle with ubuntu16.04
DataNucleus starting with Gradle
Switch between JDK 7 and JDK 8
Download Java with Ansible
Use WebJars with Gradle
Get started with Gradle
Use jlink with gradle
Download with Spring Boot
Monitor source code changes with guard-shell and make & execute automatically
Getting Started with Reactive Streams and the JDK 9 Flow API
Make Docker confusing with Pokemon and make it easier to attach
I want to make a list with kotlin and java!
I want to make a function with kotlin and java!
Make facebook messenger with talk API of api.ai and a3rt
Create Rails5 and postgresql environment with Docker and make pgadmin available