How does Gradle determine which JDK to use

I am trying to determine how Gradle determines what JDK it uses.
I need to know so that I can properly document the prerequisites for other developers who are new to Gradle.

This is on Windows.

For example I have JAVA_HOME set to the JRE “C:\Program Files (x86)\Java\jre1.8.0_73”,
Looking through the debug output it looks like Gradle is using the JDK @ “C:\Program Files (x86)\Java\jdk1.8.0_73”.

That is all well and good but how did Gradle pick that JDK? The documentation says:

Gradle uses whatever JDK it finds in your path.
Alternatively, you can set the JAVA_HOME environment variable to point to the installation directory
of the desired JDK.

However there is no JDK in the path (I removed it for testing purposes), and I have many JDKs installed (both 32 and 64bit)

λ which java
which: no java in ....path.....

Also the docs say (emphasis mine)

Gradle requires a Java JDK or JRE to be installed,

Does that mean Gradle does not require a JDK? If so then how does Gradle compile Java Source? Is it using its own internal compiler?

λ gradle -v

------------------------------------------------------------
Gradle 3.0
------------------------------------------------------------

Build time:   2016-08-15 13:15:01 UTC
Revision:     ad76ba00f59ecb287bd3c037bd25fc3df13ca558

Groovy:       2.4.7
Ant:          Apache Ant(TM) version 1.9.6 compiled on June 29 2015
JVM:          1.8.0_73 (Oracle Corporation 25.73-b02)
OS:           Windows 7 6.1 x86

Gradle does run on the JRE you specified, but it is smart enough to find the JDK next to it for compilation.

Gradle is a general-purpose build system, compiling Java is just one of its uses. Also, you can specify the path to the JDK on the Java compiler tasks directly. So Gradle can run on a JRE and still compile Java code by forking a new process for the compiler.

Hi Stefan,

Thanks for the reply. That does help. It seems that it would be a best practice (for Java build projects) to set JAVA_HOME to a JDK rather than a JRE, to avoid any potential confusion.

I understand that Gradle is a general-purpose build system that happens to run on the JVM.
However, as with any build system I assume Gradle requires a specific (external) toolchain for the target language (Java, C, C++, etc). This could be the JDK, Andriod SDK, gcc, llvm, etc.

So I was wondering how Gradle found the JDK since it wasn’t specified, which you’ve already answered. (although in looking at the source and inspecting the gradle process(es) it isn’t entirely clear how that is happening).

thanks a lot for your help!!

We’ll most probably expand this auto-discovery in the future to detect other installed JDKs on your machine so cross-compilation becomes even easier. Of course you will always have the option to specify the path explicitly on the compiler task.