native-build-tools icon indicating copy to clipboard operation
native-build-tools copied to clipboard

Gradle plugin nativeCompile fails when JAVA_HOME and GRAALVM_HOME are not set

Open msgilligan opened this issue 2 years ago • 8 comments

Describe the bug

Gradle plugin nativeCompile fails when JAVA_HOME and GRAALVM_HOME are not set.

The documentation says:

By default, the plugin will try to use the native-image tool that is bundled with the JDK that is used to run Gradle. This means you must make sure that you run Gradle with a GraalVM JDK.

But if I try a build with ~/.sdkman/candidates/java/21.0.1-graalce/bin/java and native-image in the same directory (~/.sdkman/candidates/java/21.0.1-graalce/bin/native-image), I get the following error:

> Task :consensusj-jsonrpc-cli:nativeCompile FAILED

FAILURE: Build failed with an exception.

* What went wrong:
Execution failed for task ':consensusj-jsonrpc-cli:nativeCompile'.
> Cannot query the value of property 'javaLauncher' because it has no value available.

This does not happen when JAVA_HOME or GRAALVM_HOME is set.

To Reproduce

I used the following commands to reproduce:

unset JAVA_HOME
unset GRAALVM_HOME
./gradlew consensusj-jsonrpc-cli:nativeCompile

excerpt from my subproject build.gradle:

    // Instead of using 'org.graalvm.buildtools.native' directly we need to include
    // the micronaut application plugin to avoid a Gradle bug:
    // See: https://github.com/micronaut-projects/micronaut-gradle-plugin/issues/706
    // and: https://github.com/gradle/gradle/issues/17559
    //id 'org.graalvm.buildtools.native' version '0.9.28'
    id("io.micronaut.application") version "${micronautAppGradlePluginVersion}"

gradle.properties contains:

micronautAppGradlePluginVersion = 4.2.0

Expected behavior

The nativeCompile task to complete successfully, as it does when JAVA_HOME or GRAALVM_HOME is set.

Logs

See above. I'm happy to provide more info if necessary.

System Info:

  • OS: macOS 13.6.2
  • GraalVM Version: 21.0.1-graalce (installed with SDKMAN)
  • Java Version: 21
  • Plugin version: Should be 0.9.28 (but is installed via io.micronaut.application, see above)

Additional context

I actually discovered the bug while trying to create a build that will run in a Nix shell environment.

I don't think either of these issues (or my workaround for them) is relevant:

  • https://github.com/micronaut-projects/micronaut-gradle-plugin/issues/706
  • https://github.com/gradle/gradle/issues/17559

But I thought I should link them for completeness.

msgilligan avatar Nov 25 '23 19:11 msgilligan

Interesting. I actually didn't even expect Gradle to run if JAVA_HOME isn't set :D

melix avatar Dec 06 '23 16:12 melix

This is the workaround I've been using:

graalvmNative {
  binaries {
    all {
      // native-build-tools reads this to find native-image unless either JAVA_HOME or GRAALVM_HOME are set:
      //  https://github.com/graalvm/native-build-tools/blob/0.9.28/native-gradle-plugin/src/main/java/org/graalvm/buildtools/gradle/tasks/BuildNativeImageTask.java#L211
      //  https://github.com/graalvm/native-build-tools/blob/0.9.28/native-gradle-plugin/src/main/java/org/graalvm/buildtools/gradle/internal/NativeImageExecutableLocator.java#L89
      //  https://github.com/graalvm/native-build-tools/issues/542
      javaLauncher.set(javaToolchains.launcherFor {
        // Compile with native-image from GraalVM for JDK21
        languageVersion.set(JavaLanguageVersion.of(21))
        vendor.set(JvmVendorSpec.GRAAL_VM)
      })

dsilvasc avatar Dec 20 '23 23:12 dsilvasc

@melix is this ticket still relevant?

dnestoro avatar Nov 18 '24 13:11 dnestoro

I think it is. However, as I said, it's extremely curious to run Gradle without JAVA_HOME set.

melix avatar Nov 19 '24 14:11 melix

At least on MacOS, Gradle doesn't need JAVA_HOME set. I never set JAVA_HOME, and I'm not sure any developers on a moderately recent version of MacOS ever do. The JDK installations are managed by the OS, and make sure it's on the system path, where gradlew finds it. I, too am running into this issue.

eric-creekside avatar Feb 24 '25 20:02 eric-creekside

This bug is preventing the native build from working out-of-the-box when using IntelliJ and an SDKMAN graal installation on linux.

yawkat avatar Jul 07 '25 09:07 yawkat

can confirm this still breaks. also on osx and no JAVA_HOME set and getting this exact error with 0.11.1.

maxandersen avatar Sep 27 '25 09:09 maxandersen

This bug is preventing the native build from working out-of-the-box when using IntelliJ and an SDKMAN graal installation on linux.

I get the same behavior with 1/3 of my machines. On each SDKMAN sets JAVA_HOME correctly to a current install symlink by default, but not GRAALVM_HOME. Running Gradle 8.12.1 (also SDKMAN) and build-tools 0.11.0/0.11.1. It either works in any combination or doesn't work at all

CD7567 avatar Oct 04 '25 09:10 CD7567