Java Eclipse Gradle构建“找不到tools.jar”
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/37669664/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Eclipse Gradle build "Could not find tools.jar"
提问by GMsoF
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.
我已经用谷歌搜索了一整天,并尝试了几乎所有建议的解决方案,没有一个对我的日食有效,完全不知道出了什么问题,当我尝试通过 Gradle 构建时,它一直说“找不到 tools.jar”。
What I did:
我做了什么:
1) Add Java Home (point to my JDK) in System environment variables.
1)在系统环境变量中添加Java Home(指向我的JDK)。
2) Add the path (which contain tools.jar) in the Path
system environment variables.
2)在Path
系统环境变量中添加路径(包含tools.jar)。
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")
)
3)dependencies.gradle
在项目文件夹中创建一个文件,以指示 Gradle 查找 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.
4)直接把它compile files("${System.properties['java.home']}/../lib/tools.jar")
放在build.gradle dependencies
那里。
5) In project preferences there, go Java -> Build Path -> Classpath
Variables, add in JAVA_HOME
variable.
5)在那里的项目首选项中,转到Java -> Build Path -> Classpath
变量,添加JAVA_HOME
变量。
6) Point the project build path to JDK instead of JRE.
6) 将项目构建路径指向 JDK 而不是 JRE。
None of these is working! What else I could try?
这些都不起作用!我还能尝试什么?
PS: Eclipse version Mars 4.5.2, Gradle version 1.12
PS:Eclipse 版本 Mars 4.5.2,Gradle 版本 1.12
build.gradle content (this build script is generated automatically by eclipse):
build.gradle 内容(此构建脚本由 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:
Java 主页内容:
Path environment variable:
路径环境变量:
C:\Program Files\Java\jdk1.7.0_67\lib
C:\Program Files\Java\jdk1.7.0_67\lib
Error showing in eclipse console:
在 Eclipse 控制台中显示错误:
: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.)
(堆栈跟踪太长,我缩短了它。)
采纳答案by GMsoF
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.
好的,我终于解决了我的问题。这很容易,我重新安装了JDK。不知何故,愚蠢的 Eclipse(或 Gradle 插件,我仍然不确定哪个出错了)只是无法识别我的旧 JDK 路径。
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.
在我重新安装 JDK 后(借此机会,我也将 JDK 7 升级到 8),我什至不需要 Java_home 环境变量,它可以工作。
回答by robjwilkins
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
我建议在尝试使用 eclipse gradle 构建之前让您的构建从命令行运行。看起来你的类路径有问题,但不清楚问题是什么。最初从混合中删除 eclipse 并在没有 eclipse 的情况下使构建工作会很有帮助。在命令行 cd 到 build.gradle 脚本所在的文件夹并运行
> 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.
如果这工作正常但 eclipse 构建仍然失败,那么您就会知道问题出在 eclipse 配置中的某个地方。如果从命令行使用 gradle 构建项目不起作用,那么您的项目或 gradle 设置存在潜在问题。有大量文档可以解释如何从命令行使用 gradle 构建项目。
回答by Old Badman Grey
My answer is that I needed to run Eclipse with the JDK bundled JRE instead of the standalone system JRE.
我的回答是,我需要使用 JDK 捆绑的 JRE 而不是独立系统 JRE 来运行 Eclipse。
Modify eclipse.ini
to change the vm eclipse(.exe) finds. ie:
修改eclipse.ini
以更改 vm eclipse(.exe) 发现。IE:
-vm
C:\Program Files\Java\jdk1.8.0_92\jre\bin\java.exe
回答by anij
回答by Andy Sinclair
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:
我有同样的问题。我相信这是由 gradle 配置为使用的 JRE 而不是 eclipse/STS JRE 引起的。在我的机器上,gradle 默认使用错误的 JRE。安装 gradle 后(甚至通过 buildship),它会在 Windows 上的主目录 (C:/Users/username) 中放置一个 .gradle 目录。您可以在 .gradle 中创建一个 gradle.properties 文件并指定您希望 gradle 使用的 JRE/JDK,如下例所示:
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.
这可能会通过使用 robyjwilkins 推荐的命令行方法来揭示。它应该避免需要重新安装 JDK。
For more information see https://docs.gradle.org/current/userguide/build_environment.html
有关更多信息,请参阅https://docs.gradle.org/current/userguide/build_environment.html
回答by emrhzc
In my situation in which the OP's solution didn't work either, the answer was
在我的 OP 解决方案也不起作用的情况下,答案是
- Right click on gradle task in Eclipse/Gradle Tasks plugin window
- Open tab 'Java Home'
Set Java home to C:\Program Files\Java\jdk1.8.0_131\jre
directory may vary depending on the jdk version
- 在 Eclipse/Gradle Tasks 插件窗口中右键单击 gradle 任务
- 打开选项卡“Java 主页”
将 Java 主目录设置为 C:\Program Files\Java\jdk1.8.0_131\jre
目录可能因 jdk 版本而异
回答by dvsakgec
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.
这是一个常见问题,它总是发生,因为 eclipse 已经将 JAVA_HOME 或 JDK_HOME 配置为没有 tools.jar 的 JRE 位置,因此出现问题。对于这种特殊情况:Eclipse 通常使用 Gradle(STS) 或 Buildship 插件进行 gradle 构建,因为它们使用 eclipse 提供的设置,因此您的构建失败。
Now considering above facts. There are couple of solutions which can work:
现在考虑以上事实。有几种解决方案可以工作:
- 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
- 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)
- 如果您仍想使用嵌入式 gradle,请修改 eclipse.ini 文件以提供 JDK 位置,我猜该位置可能会被插件使用,您的问题可能会得到解决。或者您甚至可以尝试为 JDK 位置配置 STS 插件
- 如果您转到 Window> Preferences>Gradle 并将其配置为使用外部 Gradle 安装,那么您可以在您的 USER 位置提供一个 gradle.properties 文件(由 @Andy Sinclair 提供)
On the similar note, You might even hit this issue even for regular Eclipse based Java project. Again solution is same.
同样,即使对于基于 Eclipse 的常规 Java 项目,您甚至可能会遇到此问题。再次解决方案是相同的。
回答by Dror
I came across this "tools.jar can't be found " issue after installing a recent java update.
在安装最近的 Java 更新后,我遇到了这个“找不到tools.jar”的问题。
Seems that Oracle doesn't bundle the tools.jar with the JRE.
似乎 Oracle 没有将 tools.jar 与 JRE 捆绑在一起。
To resolve - i copied the tools.jar from a previous version to the java/jren_nn/libfolder of the current JRE. then I restarted eclipse
要解决 - 我将以前版本的 tools.jar 复制到当前 JRE的java/jren_nn/lib文件夹中。然后我重新启动了日食
回答by sed lex
I'm using Eclipse 2019-09and i fixed this problem just by forcing Eclipse not to use the Gradle wrapper in : "windows > preferences > Gradle".
我正在使用Eclipse 2019-09,我只是通过强制 Eclipse 在以下位置不使用 Gradle 包装器来解决这个问题:“windows > 首选项 > Gradle”。
In the "Local installation directory" field, you then point to "your_gradle_folder/bin".
在“本地安装目录”字段中,然后指向“your_gradle_folder/bin”。
The Gradle preferences menu differs from an Eclipse version to another i guess reading anij answer.
Gradle 首选项菜单与 Eclipse 版本不同,我猜想阅读 anij 答案。