How To Use Specific JDK Version For Gradle Build

Introduction

Are you stuck or confused how to build a Java or Spring Boot project when your system has multiple versions of Java or JDK installed? I will show you how to use specific jdk version for gradle build.

Java version might be different in your classpath than what you want to use in your project and probably you might not have permission to change the environment variables’ value. Or you are working on multiple projects with different Java versions for those projects. Then you need to use specific Java version for building your project with Gradle build tool.

specific jdk version in gradle build

Problem Scenario

Let’s say your class path Java version shows as given below in the image:

specific java version in gradle build

Let’s say you are working on multiple projects where few of them require JDK version 1.8 or 8 but few other projects require JDK version 11.

The projects that require JDK 1.8, you can simply navigate to the project’s root directory in CLI and execute the command gradlew clean build or gradlew build to build the projects. The projects that require Jdk version 11, simply executing the same command from CLI, will fail to build, because class path knows about only Jdk version 1.8.

Solution

To solve this problem Gradle tool provides an option to pass the installed JDK’s home directory path. So, for example, you need to execute the command: gradlew build -Dorg.gradle.java.home="jdk home path", where jdk home path would be, for example, C:\Java\jdk-11.0.2. Note the path is up to installation directory’s root path only and it does not contain bin folder.

Therefore, the command will be gradlew build -Dorg.gradle.java.home=" C:\Java\jdk-11.0.2" or if you want to clean the old build before running build again: gradlew clean build -Dorg.gradle.java.home=" C:\Java\jdk-11.0.2".

That’s all about how to use specific jdk version for gradle build.

Leave a Reply

Your email address will not be published.