Gradle - 无法定位平台:“Java SE 8”使用工具链:“JDK 7 (1.7)”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/43995886/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 01:16:08  来源:igfitidea点击:

Gradle - Could not target platform: 'Java SE 8' using tool chain: 'JDK 7 (1.7)'

javaintellij-ideagradle

提问by Vasiliy Vlasov

I am trying to import Gradle project in Intellij Idea with local Gradle distrib and getting stacktrace with the following message: Could not target platform: 'Java SE 8' using tool chain: 'JDK 7 (1.7)'. Could anyone explain please what could be the reason?

我试图导入摇篮项目的IntelliJ IDEA与当地摇篮DISTRIB并得到堆栈跟踪与以下消息:Could not target platform: 'Java SE 8' using tool chain: 'JDK 7 (1.7)'。谁能解释一下可能是什么原因?

采纳答案by Vasiliy Vlasov

Finally I imported my Gradle project. These are the steps:

最后我导入了我的 Gradle 项目。这些是步骤:

  1. I switched from local Gradle distrib to Intellij Idea Gradle Wrapper (gradle-2.14).
  2. I pointed system variable JAVA_HOMEto JDK 8(it was 7th previously) as I had figured out by experiments that Gradle Wrapper could process the project with JDK 8only.
  3. I deleted previously manually created file gradle.properties(with org.gradle.java.homevariable) in windows user .gradle directory as, I guessed, it didn't bring any additional value to Gradle.
  1. 我从本地 Gradle distrib 切换到 Intellij Idea Gradle Wrapper (gradle-2.14)。
  2. 我将系统变量JAVA_HOME指向JDK 8(之前是第 7 个),因为我通过实验发现 Gradle Wrapper 只能使用JDK 8处理项目。
  3. 我删除了之前在 Windows 用户 .gradle 目录中手动创建的文件gradle.properties(带 org.gradle.java.home变量),因为我猜它没有给 Gradle 带来任何额外的价值。

回答by Xtroce

Since I had to compile some source with 7 compatibility, because of some legacy system and ran into the same problem. I found out that in the gradle configuration there where two options set to java 8

由于我不得不编译一些具有 7 兼容性的源代码,因为一些遗留系统并遇到了同样的问题。我发现在 gradle 配置中有两个选项设置为 java 8

sourceCompatibility = 1.8
targetCompatibility = 1.8

switching these to 1.7 solved the problem for me, keeping JAVA_HOME pointing to the installed JDK-7

将这些切换到 1.7 解决了我的问题,保持 JAVA_HOME 指向已安装的 JDK-7

sourceCompatibility = 1.7
targetCompatibility = 1.7

回答by Mahesh

This is what worked for me (Intellij Idea 2018.1.2):

这对我有用(Intellij Idea 2018.1.2):

1) Navigate to: File -> Settings -> Build, Execution, Deployment -> Build Tools -> Gradle

1) 导航到:文件 -> 设置 -> 构建、执行、部署 -> 构建工具 -> Gradle

2) Gradle JVM: change to version 1.8

2) Gradle JVM:更改为 1.8 版

3) Re-run the gradle task

3)重新运行gradle任务

回答by mira kabra

The following worked for me:

以下对我有用:

  1. Go to the top right corner of IntelliJ -> click the icon
  2. In the Project Structure window -> Select project -> In the Project SDK, choose the correct version -> Click Apply -> Click Okay
  1. 转到 IntelliJ 的右上角 -> 单击图标
  2. 在 Project Structure 窗口 -> Select project -> 在 Project SDK 中,选择正确的版本 -> 点击 Apply -> 点击 Okay

回答by Fran?ois Gaudin

For IntelliJ 2019:

对于 IntelliJ 2019:

Intellij IDEA -> Preferences -> Build, Execution, Deployment -> Build Tools -> Gradle -> Gradle JVM

Intellij IDEA -> Preferences -> Build, Execution, Deployment -> Build Tools -> Gradle -> Gradle JVM

Select correct version.

选择正确的版本。

回答by Aheza Desire

For IntelliJ 2019, JDK 13 and gRPC:

对于 IntelliJ 2019、JDK 13 和 gRPC:

Intellij IDEA -> Preferences -> Build, Execution, Deployment -> Build Tools -> Gradle -> Gradle JVM

Intellij IDEA -> Preferences -> Build, Execution, Deployment -> Build Tools -> Gradle -> Gradle JVM

and Select correct version.

选择正确的版本

you might also have to adding below line in your build.gradle dependencies

您可能还必须在 build.gradle依赖项中添加以下行

compileOnly group: 'javax.annotation', name: 'javax.annotation-api', version: '1.3.2'

回答by Adan Amarillas

I had a very related issue but for higher Java versions:

我有一个非常相关的问题,但对于更高的 Java 版本:

$ ./gradlew clean assemble

... <other normal Gradle output>

Could not target platform: 'Java SE 11' using tool chain: 'JDK 8 (1.8)'.

I noticed that the task succeeded when running using InteliJ. Adding a file (same level as build.gradle) called .java-versionsolved my issue:

我注意到使用 InteliJ 运行时任务成功。添加一个名为 build.gradle 的文件(与 build.gradle 相同).java-version解决了我的问题:

 # .java-version
 11.0.3