Which installed JDK used during Gradle build process
Asked Answered
K

3

10

This was my output of gradle -v (in a project using the wrapper):

$ ./gradlew -v

------------------------------------------------------------
Gradle 5.0
------------------------------------------------------------

Build time:   2018-11-26 11:48:43 UTC
Revision:     7fc6e5abf2fc5fe0824aec8a0f5462664dbcd987

Kotlin DSL:   1.0.4
Kotlin:       1.3.10
Groovy:       2.5.4
Ant:          Apache Ant(TM) version 1.9.13 compiled on July 10 2018
JVM:          11.0.1 (Oracle Corporation 11.0.1+13-LTS)
OS:           Linux 3.10.0-862.11.6.el7.x86_64 amd64

See especially this line:

JVM: 11.0.1 (Oracle Corporation 11.0.1+13-LTS)

I was wishing to switch to OpenJDK 11. So select it as you can see below:

# alternatives --config java

There are 4 programs which provide 'java'.

  Selection    Command
-----------------------------------------------
*  1           /usr/java/jdk-11.0.1/bin/java
 + 2           /usr/local/jdk-11.0.1/bin/java
   3           /usr/java/jre1.8.0_191-i586/bin/java
   4           /usr/java/jdk1.8.0_191-amd64/jre/bin/java

Enter to keep the current selection[+], or type selection number: 2

# java -version
openjdk version "11.0.1" 2018-10-16
OpenJDK Runtime Environment 18.9 (build 11.0.1+13)
OpenJDK 64-Bit Server VM 18.9 (build 11.0.1+13, mixed mode)

But there is no difference in gradle -v output. So I searched the web and find some ways (see here):

  1. Editing gradle.properties file

  2. Using -Dorg.gradle.java.home command line option

  3. Editing build.gradle file

I used the first two ways. Both worked (to test I switched to JDK 8 and then run build task. The task failed due to some new features in my codes that aren't supported by Java 8). But the result of gradle -v remained unchanged still! Even using the second way:

# ./gradlew -Dorg.gradle.java.home=/usr/java/jdk1.8.0_191-amd64 -v

------------------------------------------------------------
Gradle 5.0
------------------------------------------------------------

Build time:   2018-11-26 11:48:43 UTC
Revision:     7fc6e5abf2fc5fe0824aec8a0f5462664dbcd987

Kotlin DSL:   1.0.4
Kotlin:       1.3.10
Groovy:       2.5.4
Ant:          Apache Ant(TM) version 1.9.13 compiled on July 10 2018
JVM:          11.0.1 (Oracle Corporation 11.0.1+13-LTS)
OS:           Linux 3.10.0-862.11.6.el7.x86_64 amd64

So the question is how to check which JDK version is used by Gradle during build process?

Knowing answered 13/12, 2018 at 12:9 Comment(0)
L
11

You can add a task that prints what you need when executed (Kotlin DSL):

tasks {
    val j by creating {
        doLast {
            println(System.getProperty("java.home"))
        }
    }
}

Groovy DSL:

tasks.register("j") {
    doLast {
        println System.getProperty("java.home")           
    }
}

Then executing ./gradlew j:

/usr/lib/jvm/java-8-openjdk/jre

Why could gradlew use another JVM? Take a look at this script and you'll see that it uses JAVA_HOME variable to search for JVM. So probably the version from your PATH is not the same, that JAVA_HOME is pointing to.

Loud answered 13/12, 2018 at 14:5 Comment(0)
K
8

I find an alternative way (except @madhead answer) just for when you use a Gradle daemon:

First, find PID of daemon by running gradlew --status (see here for more information). Sample output:

   PID STATUS   INFO
 11432 IDLE     5.0

Only Daemons for the current Gradle version are displayed. See https://docs.gradle.org/5.0/userguide/gradle_daemon.html#sec:status

Then use PID to find which JDK is used:

ll /proc/<PID>/exe

Sample output:

lrwxrwxrwx. 1 0xy 0xy 0 Jan  5 04:03 /proc/11432/exe -> /usr/local/jdk-11.0.1/bin/java

On Windows:

> wmic process where "processId=<PID>" get Name, ProcessID, ExecutablePath

Sample output:

ExecutablePath                                     Name      ProcessId
C:\Program Files\Java\openjdk-11.0.1\bin\java.exe  java.exe  11432 
Knowing answered 5/1, 2019 at 0:51 Comment(1)
if there's anyone struggling with this on macOs: ps xuwww -p <PID>. that should also give you the command (including the java path) used by gradleWingback
N
0

Similar to @madhead answer, it is also possible to configure Gradle to show the used JDK (Java) version during the build process for each executed task (including the started task name).

Groovy DSL:

tasks.configureEach {
    t -> t.doFirst {
            println("Task $name started driven by " + System.getProperty("java.home").toString())
     }
 }

The advantage of such implementation is that there is no need to execute separate commands to check the used Java JDK version and path. You may have different JDK configured in the command line and other used by you IDE. Then, the above solution will work without additional efforts whenever you run any Gradle task.

Nathanialnathaniel answered 10/6 at 18:45 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.