Force Gradle to use specific Java JDK Version

If you want your Gradle tool to make use of a specific version of Java JDK, then you can follow the below steps.

Option 1:

  1. Open your Java Project that is based on Gradle Build.
  2. Make sure to set the JAVA_HOME environment variable to the JDK version you want to use.
  3. Now open the build.gradle file.
  4. Add the below lines.
    tasks.withType(JavaCompile) {
        options.fork = true
        options.forkOptions.executable = "${System.getenv('JAVA_HOME')}/bin/javac"

Option 2:

If you wish to not to change any configuration, you can do that by using the terminal to run the gradle build command with a -D option to force set the java.home temporarily.

./gradlew build -Djava.home=/path-to-your-java-jdk/
Gradle to use specific Java JDK Version

Facing issues? Have Questions? Post them here! I am happy to answer!

Author Info:

Rakesh (He/Him) has over 14+ years of experience in Web and Application development. He is the author of insightful How-To articles for Code2care.

Follow him on: X

You can also reach out to him via e-mail:

Copyright © Code2care 2024 | Privacy Policy | About Us | Contact Us | Sitemap