0

I have jvm toolchain configured as follows:

kotlin {
    jvmToolchain {
        languageVersion.set(JavaLanguageVersion.of(17))
        vendor.set(GRAAL_VM)
    }
}

My Gradle config:

gradle config

However, if I run

When I run nativeRun task, I get:

Execution failed for task ':nativeCompile'.

> Determining GraalVM installation failed with message: 'gu' at '/Users/user/.sdkman/candidates/java/current/bin/gu' tool wasn't found. This probably means that JDK at isn't a GraalVM distribution.

the output of java -version in the project console:

$ java -version
openjdk version "17.0.3" 2022-04-19 LTS
OpenJDK Runtime Environment (build 17.0.3+7-LTS)
OpenJDK 64-Bit Server VM (build 17.0.3+7-LTS, mixed mode, sharing)

an when running nativeCompile task:

> Task :nativeCompile
[native-image-plugin] GraalVM Toolchain detection is disabled
[native-image-plugin] GraalVM location read from environment variable: JAVA_HOME
[native-image-plugin] Native Image executable path: /Users/user/.sdkman/candidates/java/20.0.1-graal/lib/svm/bin/native-image

How should I make the Gradle task honor the JVM toolchain? I do not want to set Graal VM as default in the SDKMAN.

I run nativeRun task from IntelliJ (from Gradle tab).

pixel
  • 24,905
  • 36
  • 149
  • 251
  • How exactly are you running `nativeRun` from IntelliJ? Are you using a button in the Gradle tab? Are you using the `Run anything` menu? Something else? What do you mean with Gradle not respecting the toolchain? Which toolchain? Why _should_ it respect that? – dan1st Jul 06 '23 at 09:12
  • @dan1stisonstrike I clarified this – pixel Jul 06 '23 at 11:23
  • Still: What do you mean with Gradle not respecting the toolchain? Which toolchain? Why _should_ it respect whatever you are expecting to respect? – dan1st Jul 06 '23 at 11:54
  • I have jvmToolchain defined in my build.gradle.kts – pixel Jul 06 '23 at 11:56
  • And what should it do with that? What do you expect it to do, what happens and how is that different? If you want Gradle to automatically choose a toolchain, make sure it [deltects](https://docs.gradle.org/current/userguide/toolchains.html#sec:auto_detection) that and check with `gradle -q javaToolchains`. Note that I _think_ that the JVM configured in IntelliJ overrides the Gradle Toolchain configuration- – dan1st Jul 06 '23 at 15:17

1 Answers1

0

While you might have configured to use GraalVM for Gradle in IntelliJ, it seems like Gradle isn't picking that up when you run nativeRun.

If you run Gradle from some terminal, the IntelliJ setting doesn't matter at all. The IntelliJ setting is for running Gradle from inside IntelliJ (e.g. when building/running the application).

Environment variables

You can configure environment variables to tell Gradle which JVM to use.

By setting JAVA_HOME, Gradle should pick up the specified location. If you run export JAVA_HOME=/path/to/graalvm/installation before running Gradle, it should use that installation. This will not affect anything outside the terminal and you would need to set the environment variable again when you want to run Gradle using GraalVM again.

Another option is to use GRAALVM_HOME As mentioned by the docs of native-build-tools (assuming you are using that), the native Gradle plugin should respect a GRAALVM_HOME environment variable.

export JAVA_HOME=/path/to/graalvm/installation

You can also persist this environment variable on your system as it will not affect Java applications not looking for GraalVM specifically.

Alternatively, you could try to change the PATH to include the bin directory of your GraalVM installation which includes the gu binary:

export PATH=/path/to/graalvm/installation/bin:$PATH

settings.gradle

Gradle also allows specifying the JDK to use for building using the settings.gradle.

For this, you create a settings.gradle in your project directory and set org.gradle.java.home:

# inside the settings.gradle
org.gradle.java.home=/path/to/graalvm/installation

You should also be able to set the GRAALVM_HOME this way:

# inside the settings.gradle
org.gradle.project.GRAALVM_HOME=/path/to/graalvm/installation
dan1st
  • 12,568
  • 8
  • 34
  • 67
  • Do you know how to enable graal toolchain detection? – pixel Jul 05 '23 at 14:43
  • What exactly do you mean with 'enabling' Graal toolchain detection? – dan1st Jul 05 '23 at 15:42
  • See logs I added in my question – pixel Jul 05 '23 at 17:13
  • The logs seem like Gradle successfully found GraalVM using the `JAVA_HOME` variable. How does this not match your expectations? If you want Gradle to use a different JVM and use GraalVM only for `nativeCompile`, try using `GRAALVM_HOME` instead of `JAVA_HOME`. – dan1st Jul 05 '23 at 17:57
  • I would expect that if somehow GraalVM Toolchain detection would be enabled then it would honor jvmToolchain configuration. Also, I edited my question to emphasize that I run :nativeRun from IntelliJ (not from the console). – pixel Jul 06 '23 at 07:45