Gradle daemon doesn't use correct java_home when run in a java process

Hello,

When i run gradle in a java process using ProcessBuilder, and set the environment like JAVA_HOME=path to jdk8,

Here is my java code to run the subprocess gradle:

 String gradleCommand = "./gradlew clean build --info";
 ProcessBuilder processBuilder = new ProcessBuilder(gradleCommand.split("\\s+"));
 processBuilder.environment().put("JAVA_HOME", "/usr/lib/jvm/java-8-openjdk");

I got some error like this:

Starting process 'Gradle build daemon'. Working directory: /home/gradle/daemon/5.4.1 Command: /opt/java/openjdk/bin/java --add-opens java.base/java.util=ALL-UNNAMED --add-opens java.base/java.lang=ALL-UNNAMED --add-opens java.base/java.lang.invoke=ALL-UNNAMED --add-opens java.prefs/java.util.prefs=ALL-UNNAMED -Xmx1536m -Dfile.encoding=UTF-8 -Duser.country=US -Duser.language=en -Duser.variant -cp /home/gradle/wrapper/dists/gradle-5.4.1-all/3221gyojl5jsh0helicew7rwx/gradle-5.4.1/lib/gradle-launcher-5.4.1.jar org.gradle.launcher.daemon.bootstrap.GradleDaemon 5.4.1
Successfully started process 'Gradle build daemon'
An attempt to start the daemon took 0.722 secs.
The client will now receive all logging from the daemon (pid: 2300). The daemon log file: /home/gradle/daemon/5.4.1/daemon-2300.out.log
Starting build in new daemon [memory: 1.6 GB]
Using 6 worker leases.
Starting Build
java.lang.NoClassDefFoundError: Could not initialize class org.codehaus.groovy.vmplugin.v7.Java7

I tired to undestand why, my java process is run in the jvm with a jdk17, but when i check the process:

/home $ ps ax |grep java
  673 spring    0:04 /opt/java/openjdk/bin/java --add-opens java.base/java.util=ALL-UNNAMED --add-opens java.base/java.lang=ALL-UNNAMED --add-opens java.base/java.lang.invoke=ALL-UNNAMED --add-opens java.prefs/java.util.prefs=ALL-UNNAMED -Xmx1536m -Dfile.encoding=UTF-8 -Duser.country=US -Duser.language=en -Duser.variant -cp /home/gradle/wrapper/dists/gradle-5.4.1-all/3221gyojl5jsh0helicew7rwx/gradle-5.4.1/lib/gradle-launcher-5.4.1.jar org.gradle.launcher.daemon.bootstrap.GradleDaemon 5.4.1

it doesn’t use the correct JAVA_HOME that i set in the ProcessBuilder:
processBuilder.environment().put("JAVA_HOME", "/usr/lib/jvm/java-8-openjdk");

What’s wrong with that ? It seems that the gradle client jvm dont use the same JAVA_HOME as the gradle daemon jvm, the gradle client jvm use the correct path, if i echo “JAVACMD” in the gradlew file, i can get:
here is java path: /usr/lib/jvm/java-8-openjdk/bin/java

Does anyone may have a idea for that ?

By the way, when i change my jdk to 11 globally, everything is ok

Thank you very much for your help,

For anyone who have the same problem, it have troubled me 1 weeks and not resolved:

There is a known issue for alpine and gradle when build with different jdk inside one docker image: Toolchain feature detects Java 8 JDK as a different Java version when inside an Alpine Linux container. · Issue #24300 · gradle/gradle · GitHub

To resolve such problem, For now, gradle propose to change alpine to ubuntu: Add a note to documentation about toolchains limitations with musl library by ov7a · Pull Request #24511 · gradle/gradle · GitHub