Gradle build in Eclipse: Can not find tools.jar

When trying to build Gradle project in Eclipse , get this error

 can not find tools jar gradle eclipse

In details, the message shows:

Could not find tools.jar
Could not execute build using Gradle installation 'C:\App\gradle-2.5'.

I noticed that it only gets this error when Gradle need to compile java source files, other tasks not affected. And there are no problems when calling Gradle from command line.

The tools.jar file is part of the JDK, I think it maybe the problem of the environment variables like JAVA_HOME or CLASSPATH. I rechecked the environment variable setting to make sure its all right, but still get the error. Obviously, its not about environment variable, when building Gradle from Eclipse , it never reference the JDK pointed by environment variable.

The JVM used by Gradle is specified in Installed JRE.

 installed JRE eclipse

Actually Eclipse doesn't need JDK to run or compile java source files, because Eclipse has built in Java compiler, but Gradle can not use that built-in compiler.

Another confusion is the name "Installed JRE", sounds like it must be a JRE installation and JDK and JRE are quite two kind of things. Actually it also can be a JDK installation , so the name may better be "Installed JRE or JDK".

 install JDK eclipse

After adding the JDK , select the checkbox of the newly installed JDK to activate it, now Gradle will use this JDK and everything is OK.

Which jre Gradle are using

When running Gradle in Eclipse, it will find jre throgh the java.home property:

task debug << {
    println "java.home is " +['java.home']

Select only the task debug and run Gradle will print the value of this property.

If the output is a jre directory, then it definitely will cause "can not find tools.jar" problem. And I find even you add JDK installation to Installed JRE and activate it, the java.home property still maybe wrong.

Change the Installed JRE didn't completely solve the problem, I still get the error sometimes.

After some digging and testing, I found the value of this property depend on which JVM is used to launch Eclipse, by default if there is a folder named 'jre' in the same folder with eclipse.exe, then this jvm will be used, and in this case, the java.home property always point to this jre folder.

If we delete or rename the jre folder and launch eclipse.exe from command line, java.home will point to whichever jdk or jre that activated in current command line environment.

For example, on Windows, if the PATH environment variable contains the JDK bin directory C:\Program Files (x86)\Java\jdk1.7.0_17\bin, then the java.home will be C:\Program Files (x86)\Java\jdk1.7.0_17\jre, in this case, there will be no error.

The reason my Eclipse using the jre folder in the same directory is the Eclipse is copied from another machine, If the local machine didn't install JDK, the Eclipse can still launch, it will automatically use the JVM in jre folder, see How to solve a JRE or JDK must be available in order to run Eclipse. But this will break some build plugins like Gradle, so the best solution is always prefer a real JDK installation, when the JDK is not available , use the portable jre.

If changing installed JRE don't solve the problem, first check the java.home property and then try to launch eclipse with a JDK installation.