How do I tell Gradle to use specific JDK version?
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
Introduction
The modern way to tell Gradle to use a specific JDK version is with Java Toolchains, available since Gradle 6.7. Toolchains let you declare the required Java version in your build script, and Gradle automatically locates (or downloads) the right JDK. This article covers toolchains and every other method, so you can pick the right approach for your situation.
Method 1: Java Toolchains (Recommended)
Java Toolchains decouple the JDK used to run Gradle from the JDK used to compile your code. You declare the language version in your build script, and Gradle does the rest.
build.gradle (Groovy DSL):
build.gradle.kts (Kotlin DSL):
When you run ./gradlew build, Gradle will:
- Search for a JDK 21 installation on your machine (checking standard locations and environment variables)
- If not found, automatically download one (from Adoptium by default, since Gradle 7.6)
- Use that JDK for compilation, test execution, and Javadoc generation
Specifying Vendor and Implementation
You can be more specific about which JDK distribution to use:
Verifying Which Toolchain Gradle Selected
Example output:
Method 2: JAVA_HOME Environment Variable
Setting JAVA_HOME tells Gradle which JDK to use for running Gradle itself and (by default) compiling code.
Linux / macOS:
Windows (Command Prompt):
Windows (PowerShell, permanent):
Verify it took effect:
The output shows "JVM" with the Java version Gradle is using.
When to use JAVA_HOME vs. Toolchains
JAVA_HOME controls the JDK that runs the Gradle daemon. Toolchains control the JDK that compiles your code. These can be different. For example, you can run Gradle on Java 21 while compiling your project with Java 17 via toolchains.
Method 3: gradle.properties
The org.gradle.java.home property overrides JAVA_HOME for Gradle specifically:
gradle.properties (project root):
Per-user default (in ~/.gradle/gradle.properties):
The project-level file takes precedence over the user-level file.
From the command line (one-off):
Important caveat
Do not commit a gradle.properties with a hardcoded absolute path. Paths vary across machines and operating systems. Use toolchains instead for portable builds, and reserve org.gradle.java.home for local overrides.
Method 4: sourceCompatibility and targetCompatibility (Cross-Compilation)
These settings tell the Java compiler to produce bytecode compatible with a specific Java version, without requiring a different JDK installation:
Or equivalently in tasks:
The --release flag is better than sourceCompatibility/targetCompatibility because it also enforces API compatibility. With just sourceCompatibility = 11, you could accidentally use Java 21 APIs that do not exist in Java 11. The --release 11 flag prevents that.
Method 5: Per-Task JDK Configuration
For fine-grained control, you can set the JDK on individual tasks:
This is useful when you need to compile with JDK 17 but run tests against JDK 21 to verify forward compatibility.
Gradle and JDK Compatibility Reference
Not all Gradle versions support all JDK versions. Here is the compatibility:
| Gradle Version | Minimum JDK | Maximum JDK | Toolchain Support |
| 8.10+ | 8 | 23 | Yes |
| 8.5-8.9 | 8 | 22 | Yes |
| 8.0-8.4 | 8 | 20 | Yes |
| 7.6 | 8 | 19 | Yes (auto-download) |
| 7.0-7.5 | 8 | 18 | Yes (manual install) |
| 6.7-6.9 | 8 | 16 | Yes (basic) |
| 6.0-6.6 | 8 | 15 | No |
If you are on an older Gradle, upgrade the Gradle Wrapper first:
Complete Working Example
Here is a full build.gradle.kts for a project that requires JDK 21:
Running ./gradlew run on this project will automatically use JDK 21, downloading it if necessary.
Common Pitfalls
- Confusing the Gradle JVM with the compilation JDK. Gradle itself runs on one JDK (set by
JAVA_HOMEororg.gradle.java.home), but your project can compile with a completely different JDK via toolchains. These are separate concerns. - Committing absolute paths in gradle.properties. This breaks builds on other machines. Use toolchains for reproducible builds across different environments.
- Setting sourceCompatibility without --release.
sourceCompatibility = 11with a JDK 21 compiler lets you use Java 21 APIs that do not exist in Java 11. The code compiles but fails at runtime. Useoptions.release = 11instead. - Forgetting to update the Gradle Wrapper. Old Gradle versions do not support new JDKs. Always check the Gradle-JDK compatibility table and update the wrapper if needed.
- IDE using a different JDK than Gradle. IntelliJ, Eclipse, and VS Code each have their own JDK settings. If your IDE compiles fine but
./gradlew buildfails, check that the IDE imports the Gradle project's toolchain configuration. - CI/CD not having the required JDK installed. If your CI server does not have JDK 21 and toolchain auto-download is disabled (which is the default in some environments), the build fails. Either pre-install the JDK or enable auto-provisioning with the Gradle Toolchain Resolver plugin.
Summary
- Use Java Toolchains (Gradle 6.7+) for portable, reproducible JDK configuration. Declare
languageVersionin your build script. - Use
JAVA_HOMEto control which JDK runs the Gradle daemon. - Use
org.gradle.java.homeingradle.propertiesfor local overrides, but never commit absolute paths. - Use
options.releaseinstead ofsourceCompatibility/targetCompatibilityfor cross-compilation to prevent accidental use of newer APIs. - Verify your setup with
./gradlew -q javaToolchainsand./gradlew --version. - Check the Gradle-JDK compatibility table before upgrading either Gradle or your JDK.
Related reading
- How do I tell Maven to use the latest version of a dependency?
- How do I tell Spring Boot which main class to use for the executable jar?
- How do I tell Spring Boot which main class to use for the executable jar?
- How do I time a method's execution in Java?
- How do I trim a file extension from a String in Java?
- How do I turn a String into a InputStreamReader in java?
- How do I update an entity using spring-data-jpa?
- How do I update the element at a certain position in an ArrayList?

OOD Fundamentals
Master object-oriented design from first principles, SOLID, design patterns, and classic interview problems with hands-on coding.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.