Explain Codes LogoExplain Codes Logo

Gradle does not find tools.jar

java
gradle
jdk
environment-variables
Alex KataevbyAlex Kataev·Nov 22, 2024
TLDR

Rescue your Gradle project by switching from a JRE to a JDK.

  1. Download and install a JDK (not a JRE).
  2. Set JAVA_HOME to the path to your installed JDK. No /bin needed at the end.
  3. Restart your terminal or IDE to get JAVA_HOME in play.
  4. Check that Gradle's Java version is twinning with your JDK version.

Unix-like system JAVA_HOME setup:

export JAVA_HOME=/your/jdk/path

Windows JAVA_HOME setup:

set JAVA_HOME=C:\your\jdk\path

Then, fire up Gradle again. Fingers crossed for a successful build!

Unraveling the mystery

The elusive tools.jar file is part of the JDK and not the JRE. Its job is critical - it's the all-important toolkit that Grade uses to compile, debug, and run your Java code. So if Gradle can't find it, we have a problem. But don't worry, we can sort it out!

Detailed guide for the lost souls

Set the right JDK

Confirm the JDK installation and the available tools.jar in the JDK's lib directory.

Windows: C:\Program Files\Java\jdk<version>\lib\tools.jar Linux/macOS: /usr/lib/jvm/java-<version>-openjdk/lib/tools.jar
# because we love checking twice ls ${JAVA_HOME}/lib/tools.jar

Don't let ENV variables catch you by surprise

Update your environment variables. The key player here is JAVA_HOME which has to point to your JDK's install directory.

Also, remember kids: your system's PATH must include the JDK's bin folder. On Unix systems, update your ~/.bashrc or ~/.zshrc file for this change to stick around.

Gradle has a say too

In your project's gradle.properties file, change org.gradle.java.home to your JDK path. This way, Gradle knows for sure where to look for the JDK.

Check your IDE

Make sure the java.home system property in your IDE settings or build script is correctly referencing the JDK. In IntelliJ IDEA, you can check this under Project Structure settings.

Eclipse users, rejoice!

In Eclipse, check Gradle Run Configurations and set Java Home to your JDK's home.

A few more pointers

  1. Be sure to reopen your IDE or terminal after modifying environment variables.
  2. When mentioning the JDK path in property files, remember, Windows loves backslashes (\).
  3. Still don't have JDK? Use apt or yum to install an OpenJDK version.
  4. If you recently installed or upgraded JDK, you might want to restart any ongoing Java-related services to get them aligned with the new setup.

The tale of system properties

JAVA_HOME vs java.home

JAVA_HOME is an environment variable referring to the JDK directory. java.home is called often by JVM and Java applications to locate the Java directory. The rule of thumb is to aim both towards your JDK installation.

Gradle and JDK: A love story

Gradle loves the tools from JDK (including tools.jar). If Gradle cannot find tools.jar, it means it's not correctly aiming at your JDK, or your JDK was not installed correctly. Don't worry, we can get them back together!

Tips to dodge common errors

Suddenly, tools.jar goes MIA

Sometimes, upgrading JDK but keeping an old JRE can confuse the poor Gradle. Double-check to ensure Gradle is referencing your shiny-new JDK and not the rusty-old JRE.

JAVA_HOME snobs

Some IDEs may need a nudge to pick up environment variable changes. Update your JDK path in the IDE's build configuration if Gradle tasks run from the IDE keep complaining.

The devil is in the details

On Windows, remember the path needs backslashes (\). Remember to escape them in your property files. The double backslashes (\\) are your friend.

Permission denied (Unix purgatory)

On Unix-like systems, permission issues can block Gradle's access to JDK. Use sudo to fix these. And then pray for forgiveness.