How to check which installed JDK used during Gradle build process

和自甴很熟 提交于 2020-01-11 13:04:34

问题


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?


回答1:


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.




回答2:


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 


来源:https://stackoverflow.com/questions/53761545/how-to-check-which-installed-jdk-used-during-gradle-build-process

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!