Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to check which installed JDK used during Gradle build process

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?

like image 664
Mir-Ismaili Avatar asked Dec 13 '18 12:12

Mir-Ismaili


People also ask

Which JDK does Gradle use?

Gradle requires a Java JDK or JRE to be installed, version 6 or higher (to check, use java -version ). Gradle ships with its own Groovy library, therefore Groovy does not need to be installed. Any existing Groovy installation is ignored by Gradle. Gradle uses whatever JDK it finds in your path.

How does Gradle know which Java?

Gradle uses whichever JDK it finds in your path (to check, use java -version). Alternatively, you can set the JAVA_HOME environment variable to point to the install directory of the desired JDK.

How do I select a JDK in Gradle?

Set the JDK versionOpen your project in Android Studio and select File > Settings... > Build, Execution, Deployment > Build Tools > Gradle (Android Studio > Preferences... > Build, Execution, Deployment > Build Tools > Gradle on a Mac). Under Gradle JDK, choose the Embedded JDK option.


Video Answer


1 Answers

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.

like image 135
madhead - StandWithUkraine Avatar answered Oct 10 '22 06:10

madhead - StandWithUkraine