How do I tell Gradle to use specific JDK version? How do I tell Gradle to use specific JDK version? java java

How do I tell Gradle to use specific JDK version?


Two ways

  1. In gradle.properties in the .gradle directory in your HOME_DIRECTORY set org.gradle.java.home=/path_to_jdk_directory

or:

  1. In your build.gradle

     compileJava.options.fork = true compileJava.options.forkOptions.executable = '/path_to_javac'


If you add JDK_PATH in gradle.properties your build become dependent on on that particular path.Instead Run gradle task with following command line parametemer

gradle build -Dorg.gradle.java.home=/JDK_PATH

This way your build is not dependent on some concrete path.


To people ending up here when searching for the Gradle equivalent of the Maven property maven.compiler.source (or <source>1.8</source>):

In build.gradle you can achieve this with

apply plugin: 'java'sourceCompatibility = 1.8targetCompatibility = 1.8

See the Gradle documentation on this.