Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Eclipse Gradle build "Could not find tools.jar"

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.)

like image 296
Sam YC Avatar asked Jun 07 '16 02:06

Sam YC


4 Answers

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.

like image 147
Sam YC Avatar answered Oct 10 '22 17:10

Sam YC


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

like image 31
Andy Avatar answered Oct 10 '22 18:10

Andy


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.

like image 37
dvsakgec Avatar answered Oct 10 '22 17:10

dvsakgec


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

like image 22
anij Avatar answered Oct 10 '22 16:10

anij