In this tutorial, we will be using the Badass Runtime Gradle plugin. Behind the scenes, it uses the jlink JDK tool to generate the final slim runtime image.

Let's begin with this simple Java project that just prints Hello World ! :

Gradle configuration

Apply the Badass Runtime plugin to your project's build.gradle :

plugins { // [...] id "org.beryx.runtime" version "1.3.0" }

(The latest version can be found here)

Append the following block to your build.gradle :

runtime { options = ['--strip-debug', '--compress', '2', '--no-header-files', '--no-man-pages'] }

If you have any resources, they must be copied to the generated runtime image by appending the following block to the build.gradle :

tasks.runtime.doLast { copy { from('src/main/resources') into("$buildDir/image/bin") } }

Generate the custom runtime image

In this example, the JAVA_HOME environment variable is assumed to be pointed at a JDK 11 (The following section will describe how to use another JDK).

From the command line, run gradlew runtime (or gradle runtime when using a global Gradle installation)

The build/image directory will be generated.

This directory contains all the necessary modules and files to run the Java app without needing to install a JDK or JRE in the user's workstation.