14
votes

I have googled for whole day, and tried almost all the suggested solutions, none is working for my eclipse, totally have no idea what went wrong, it kept saying "Could not find tools.jar" when I try to build via Gradle.

What I did:

1) Add Java Home (point to my JDK) in System environment variables.

2) Add the path (which contain tools.jar) in the Path system environment variables.

3) Create a dependencies.gradle files in project folder, to instruct Gradle to look for tools.jar (compile files("${System.properties['java.home']}/../lib/tools.jar"))

4) Directly put the compile files("${System.properties['java.home']}/../lib/tools.jar") in the build.gradle dependencies there.

5) In project preferences there, go Java -> Build Path -> Classpath Variables, add in JAVA_HOME variable.

6) Point the project build path to JDK instead of JRE.

None of these is working! What else I could try?

PS: Eclipse version Mars 4.5.2, Gradle version 1.12

build.gradle content (this build script is generated automatically by eclipse):

apply plugin: 'java'
apply plugin: 'eclipse'

sourceCompatibility = 1.5
version = '1.0'
jar {
    manifest {
        attributes 'Implementation-Title': 'Gradle Quickstart',
                   'Implementation-Version': version
    }
}

repositories {
    mavenCentral()
}

dependencies {
    compile group: 'commons-collections', name: 'commons-collections', version: '3.2'
    testCompile group: 'junit', name: 'junit', version: '4.+'
}

test {
    systemProperties 'property': 'value'
}

uploadArchives {
    repositories {
       flatDir {
           dirs 'repos'
       }
    }
}

The Java Home content:

enter image description here

Path environment variable:

C:\Program Files\Java\jdk1.7.0_67\lib

Error showing in eclipse console:

:compileJava FAILED

FAILURE: Build failed with an exception.

* What went wrong:
Execution failed for task ':compileJava'.
> Could not find tools.jar

* Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output.

BUILD FAILED

Total time: 0.152 secs
[sts] Build failed
org.gradle.tooling.BuildException: Could not execute build using Gradle 
installation 'C:\buildtools\gradle-2.12'.
Caused by: java.lang.IllegalStateException: Could not find tools.jar

(stacktrace too long, I shorten it.)

9
Update this showing exactly where you installed the JDK, and what JAVA_HOME is set to. Remove the dependency for "tools.jar". You don't need that. Show your entire build script and the build result.David M. Karr
@DavidM.Karr thanks for the advice, I have updated the question,Sam YC
Path has to go to bin, not lib.David M. Karr
@DavidM.Karr I have tried that, still the same error. Anyway, the tools.jar is in lib folder, not in bin folder.Sam YC

9 Answers

7
votes

OK, I got my problem solved finally. It is very easy, I re-installed the JDK. Somehow the stupid Eclipse (or Gradle plugin, I still not sure which goes wrong) just can't recognize my old JDK path.

After I re-installed JDK (take this chance, I upgrade JDK 7 to 8 also), I don't even need Java_home environment variable, it works.

4
votes

Step 1: open eclipse.

Step 2: open Window -> Preferences

Step 3: go to Gradle (STS) -> Arguments

Step 4: choose proper JDK version for Java Home option. (see image below)

enter image description here

4
votes

I had the same problem. I believe it is caused by the JRE that gradle is configured to use rather than the eclipse/STS JRE. On my machine, gradle had defaulted to using the wrong JRE. When gradle is installed (even via buildship) it puts a .gradle directory in your home directory (C:/Users/username) on Windows. You can create a gradle.properties file in .gradle and specify the JRE/JDK you want gradle to use as in this example:

org.gradle.java.home=C:/Program Files/Java/jdk1.8.0

This would probably be revealed by using the command line approach recommended by robyjwilkins. It should avoid the need to re-install a JDK.

For more information see https://docs.gradle.org/current/userguide/build_environment.html

2
votes

I would recommend getting your build to run from the command line before trying to use the eclipse gradle build. It looks like there is a problem with your classpath, but its not clear exactly what the problem is. It would be helpful to initially remove eclipse from the mix and get the build working without eclipse. At the command line cd to the folder where the build.gradle script is stored and run

> gradle clean build

or

> ./gradlew clean build

If this works ok but the eclipse build still fails than you will know that the problem lies somewhere in your eclipse config. If building the project with gradle from the command line does not work then there is an underlying problem with you project or gradle setup. There is plenty of documentation available which will explain how to build the project with gradle from the command line.

2
votes

My answer is that I needed to run Eclipse with the JDK bundled JRE instead of the standalone system JRE.

Modify eclipse.ini to change the vm eclipse(.exe) finds. ie:

-vm
C:\Program Files\Java\jdk1.8.0_92\jre\bin\java.exe
2
votes

In my situation in which the OP's solution didn't work either, the answer was

  1. Right click on gradle task in Eclipse/Gradle Tasks plugin window

  2. Open tab 'Java Home'

  3. Set Java home to C:\Program Files\Java\jdk1.8.0_131\jre

    directory may vary depending on the jdk version

2
votes

This is a common problem, It happens always because eclipse has got JAVA_HOME or JDK_HOME configured to JRE location which doesn't have tools.jar and hence the issue. For this particular case: Eclipse generally uses Gradle(STS) or Buildship plugins for gradle build, since these use eclipse provided settings so your build fails.

Now considering above facts. There are couple of solutions which can work:

  1. If you still want to use embedded gradle then modify the eclipse.ini file for providing JDK location which I guess might be used by the plugin and your issue might be resolved. OR you can even try configuring the STS plugin for JDK location
  2. If you go to Window> Preferences>Gradle and configure it to use external Gradle installation then you can provide a gradle.properties file in your USER location (sol. by @Andy Sinclair)

On the similar note, You might even hit this issue even for regular Eclipse based Java project. Again solution is same.

1
votes

I came across this "tools.jar can't be found " issue after installing a recent java update.

Seems that Oracle doesn't bundle the tools.jar with the JRE.

To resolve - i copied the tools.jar from a previous version to the java/jren_nn/lib folder of the current JRE. then I restarted eclipse

0
votes

I'm using Eclipse 2019-09 and i fixed this problem just by forcing Eclipse not to use the Gradle wrapper in : "windows > preferences > Gradle".

In the "Local installation directory" field, you then point to "your_gradle_folder/bin".

The Gradle preferences menu differs from an Eclipse version to another i guess reading anij answer.