Compile
Java
to a
native executable
using
GraalVM
on
Arch Linux
with
Gradle
Here are the steps to compile a Java "Hello, World!" program to a native executable using GraalVM on Arch Linux with Gradle:
- Download the GraalVM package from AUR, I'm using Aura for this:
aura -A jdk24-graalvm-ce-bin
- Create a regular Java project with Gradle and select the default options each time (single application project and Kotlin as the build script DSL):
gradle init --type java-application
- Modify your
build.gradle.kts
:-
Add the GraalVM plugin:
plugins { application id("org.graalvm.buildtools.native") version "0.10.6" }
-
Tell Gradle to use the GraalVM (see here for what happens if you leave this out):
graalvmNative { binaries { all { javaLauncher.set(javaToolchains.launcherFor { languageVersion.set(JavaLanguageVersion.of(24)) vendor.set(JvmVendorSpec.GRAAL_VM) }) } } }
-
Add the GraalVM plugin:
-
In
gradle.properties
, deactivate the configuration cache:org.gradle.configuration-cache=false
Doing so avoids an error starting with:Configuration cache state could not be cached: field `imageCompileOnly` of `org.graalvm.buildtools.gradle.internal.NativeConfigurations` bean found in field `capturedArgs` of `java.lang.invoke.SerializedLambda` bean found in field `transformer`- Finally, compile the project to a native executable:
./gradlew nativeCompile
The resulting executable ELF file is inapp/build/native/nativeCompile
.Using GraalVM CE 24.0.1+9.1, the executable is 13.19 MiB in size. Some compilation times I've observed:
- About 15 seconds on a Framework 16 with AMD Ryzen 9 7940HS and 30.65 GiB of RAM.
- About 2 minutes on a Lenovo X220 with Intel Core i5-2520M and 15.5 GiB of RAM.
- Finally, compile the project to a native executable: