Different ways to configure java version in Gradle project

This tutorials talks about different ways of configuring java version in gradle projects.

Gradle configure java version in projects

In java projects, Source projects are compiled and generates class files.

In Gradle, For java plugin, We need provide java version in build gradle for compile and generate class files.

Java installation is required for building gradle project and running gradle build.

How to configure source and target jdk version in gradle build

to compile files in java, we use javac command and it has source and target arguments for javac command

javac Emp.java -source 1.9 -target 1.8

source tells to compiler to compile with specific version target tells to compiler to support lowest java version to support

In the above program Emp.java code is compiled with java 1.9 version and generated class file has a compatable with 1.8 or more, but not 1.7 or less versions.

Does gradle support this properties? Yes It supports the same approach with different properties.

It has sourceCompatibility and targetCompatibility properties

So configure in build.gradle

sourceCompatibility = '1.10'
targetCompatibility = '1.8'

sourceCompatibility uses java version to compile java files targetCompatibility tells generate class supports minimum java version that has support

This options enables to run all tasks with specific versions.

Suppose if you want different versions for each task, you can configure build script as follows

tasks.withType<JavaCompile> {
    options.compilerArgs.addAll(arrayOf("--release", "12"))

The above script tell to use java version 12 to compile java files in a gradle project

How to install and configure java version with gradle build?

By default, gradle uses the java version from JAVA_HOME environment variable path configured in machine and JAVA_HOME points to jdk installed on machine.

For example, machine is installed with jdk 1.8 version and gradle project needs java 11 version.

In that case, How do you handle this.

  • install java 11 version on machine and it is straightforward
  • Gradle project has configuration to install required java version and configure.

How do you configure gradle build with java installation and configuration

with Gradle 6.7 version, It has support for java toolchains

What does java toolchain do?

  • It ignore java version on machine
  • In builg file, configure java version
  • It installs jdk if not installed
  • Gradle build runs with that version.
  • No configuration required for sourceCompatibility and targetCompatibility properties

How do you configure java toolchain in gradle project?

  • First configure plugins for java-library
  • next configure tool chain with required java version

In build.gradle file, configure below code

plugins {
    id("java-library")  //  id("application")

java {
    toolchain {

So gradle does the following things

  • Installs java 12 versions from remote locations, Locations can be configurable
  • check for valid installation
  • All java related tasks uses java 12 version for compile and generates class files
  • All test related tasks uses java 12 version
  • documentation relate tasks uses java 12 verison.

It has flexibility to configure java version at task level.

For example, Configure java Compilation requires 12 version and test requires 15 version

tasks.register<Test>("extraTests") {
    javaLauncher.set(javaToolchains.launcherFor {


In this tutorial, You learned different ways to configure java version

  • using sourceCompatibility and targetCompatibility options in build.gradle
  • java toolchain support

With above two options, You can choose to use java version different either at project level or task level with examples

Join 6,000 subscribers and get a daily digest of full stack tutorials delivered to your inbox directly.No spam ever. Unsubscribe any time.

Similar Posts
You'll get a notification every time a post gets published here.