# How does Gradle determine which JDK to use

**URL:** <https://discuss.gradle.org/t/how-does-gradle-determine-which-jdk-to-use/19896>\
**Category:** Help/Discuss\
**Created:** [October 6, 2016, 3:44pm UTC](https://discuss.gradle.org/t/how-does-gradle-determine-which-jdk-to-use/19896 "2016-10-06T15:44:50Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![raleighr3](https://sea1.discourse-cdn.com/gradle/user_avatar/discuss.gradle.org/raleighr3/32/5378_2.png) [@raleighr3](https://discuss.gradle.org/u/raleighr3)\
**Post date:** [October 6, 2016, 3:44pm UTC](https://discuss.gradle.org/t/how-does-gradle-determine-which-jdk-to-use/19896/1 "2016-10-06T15:44:50Z")

</div>

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

```

---

<div class="post-metadata">

**Author:** ![st\_oehme](https://sea1.discourse-cdn.com/gradle/user_avatar/discuss.gradle.org/st_oehme/32/5249_2.png) [@st\_oehme](https://discuss.gradle.org/u/st_oehme)\
**Post date:** [October 6, 2016, 4:44pm UTC](https://discuss.gradle.org/t/how-does-gradle-determine-which-jdk-to-use/19896/2 "2016-10-06T16:44:01Z")

</div>

> [@raleighr3](#):
>
> That is all well and good but how did Gradle pick that JDK

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

> [@raleighr3](#):
>
> 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 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.

---

<div class="post-metadata">

**Author:** ![raleighr3](https://sea1.discourse-cdn.com/gradle/user_avatar/discuss.gradle.org/raleighr3/32/5378_2.png) [@raleighr3](https://discuss.gradle.org/u/raleighr3)\
**Post date:** [October 6, 2016, 5:33pm UTC](https://discuss.gradle.org/t/how-does-gradle-determine-which-jdk-to-use/19896/3 "2016-10-06T17:33:48Z")

</div>

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!!

---

<div class="post-metadata">

**Author:** ![st\_oehme](https://sea1.discourse-cdn.com/gradle/user_avatar/discuss.gradle.org/st_oehme/32/5249_2.png) [@st\_oehme](https://discuss.gradle.org/u/st_oehme)\
**Post date:** [October 6, 2016, 7:35pm UTC](https://discuss.gradle.org/t/how-does-gradle-determine-which-jdk-to-use/19896/4 "2016-10-06T19:35:21Z")

</div>

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.
