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:

  1. Download the GraalVM package from AUR, I'm using Aura for this:
    aura -A jdk24-graalvm-ce-bin
  2. 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
  3. 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)
            })
          }
        }
      }
      
  4. 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`
  5. Finally, compile the project to a native executable:
    ./gradlew nativeCompile
    The resulting executable ELF file is in app/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:

This was tested using Gradle version 8.13 on Arch Linux 6.14.2.