错误:Java:目标版本无效:11 - IntelliJ IDEA

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

Error: Java: invalid target release: 11 - IntelliJ IDEA

javaintellij-ideajava-11

提问by AMagic

I am trying to build an application which was built using java 8, now it's upgraded to java 11. I installed Java 11using an oracle articlein my windows machineand I use IntelliJ IDEA 2017as my IDE.

我正在尝试构建一个使用 java 8 构建的应用程序,现在它升级到 java 11。我在我的Windows 机器上使用oracle 文章安装了Java 11,我使用IntelliJ IDEA 2017作为我的 IDE。

I changed my system environment variables and set the

我更改了系统环境变量并设置了

JAVA_HOME to C:\Program Files\Java\jdk-11.0.1

And added that to the Pathvariable.

并将其添加到Path变量中。

C:\>java -version
java version "11.0.1" 2018-10-16 LTS
Java(TM) SE Runtime Environment 18.9 (build 11.0.1+13-LTS)
Java HotSpot(TM) 64-Bit Server VM 18.9 (build 11.0.1+13-LTS, mixed mode) 

When I build my application in IntelliJ, this is what I get:

当我在 IntelliJ 中构建我的应用程序时,这就是我得到的:

Information:java: Errors occurred while compiling module 'test-domain_main'
Information: javac 1.8.0_171 was used to compile java sources
Information:1/10/2019 4:21 PM - Compilation completed with 1 error and 0 warnings in 1s 199ms
Error:java: invalid target release: 11

This is what I've tried so far:

这是我迄今为止尝试过的:

  1. I changed .idea/compiler.xmltarget values from 8 to 11 but that didn't help. Also, verified the Target bytecode versionin settings > Build, Execution, Deployment > Compiler > Java Compilerand all my modules are set to 11.

  2. Went to file > Project Structure > SDKs *(currently I have 1.7 and 1.7 listed)* > Add new SDK > JDK >After that, I selected C:\Program Files\Java\jdk-11.0.1But it errors out with "The selected directory is not a valid home for JDK"

  1. 我将.idea/compiler.xml目标值从 8更改为 11,但这没有帮助。此外,验证Target bytecode versioninsettings > Build, Execution, Deployment > Compiler > Java Compiler和我的所有模块都设置为 11。

  2. 去了file > Project Structure > SDKs *(currently I have 1.7 and 1.7 listed)* > Add new SDK > JDK >之后,我选择的C:\Program Files\Java\jdk-11.0.1,但它的错误了“所选目录不是JDK有效的家”

I am not sure if I installed the wrong JDK 11, because in my C:\Program Files\Java\, I see separate JDK and JRE folders for 1.7and 1.8but only JDK folder for 11.0.1

我不知道如果我安装了错误的JDK 11,因为在我C:\Program Files\Java\,我看到不同的JDK和JRE文件夹1.71.8,但只JDK文件夹11.0.1

Or is it something else I need to change?

或者还有什么我需要改变的吗?

采纳答案by CrazyCoder

Please updateto IntelliJ IDEA 2018.x to get Java 11 support. Your IntelliJ IDEA version was released before Java 11 and doesn't support this Java version.

更新到 IntelliJ IDEA 2018.x 以获得 Java 11 支持。您的 IntelliJ IDEA 版本在 Java 11 之前发布,不支持此 Java 版本。

回答by Gryu

  • I've got the same info messages and error message today, but I have recently updated Idea -> 2018.3.3, built on January 9, 2019.
  • To resolve this issue I've changed File->Project Structure->Modules->> Language level to 10.

  • And check File -> Settings -> Build, Execution, Deployment -> Compiler -> Java Compiler->> Project bytecodeand Per-module bytecodeversions. I have 11 there.

  • Now I don't get these notifications and the error.

    It could be useful for someone like me, having the most recent Idea and getting the same error.

  • 我今天收到了相同的信息消息和错误消息,但我最近更新了 Idea -> 2018.3.3,构建于 2019 年 1 月 9 日。
  • 为了解决这个问题,我将File->Project Structure->Modules->> Language level 更改为 10。

  • 并检查File -> Settings -> Build, Execution, Deployment -> Compiler -> Java Compiler-> Project bytecodeand Per-module bytecodeversions。我那里有11个。

  • 现在我没有收到这些通知和错误。

    对于像我这样拥有最新想法并遇到相同错误的人来说,这可能很有用。

回答by Paolo

I've got the same issue like stated by Grigoriy Yuschenko. Same Intellij 2018 3.3

我遇到了 Grigoriy Yuschenko 所说的同样问题。同样的 Intellij 2018 3.3

I was able to start my project by setting (like stated by Grigoriy)

我能够通过设置开始我的项目(如 Grigoriy 所述)

File->Project Structure->Modules ->> Language level to 8 ( my maven project was set to 1.8 java)

文件->项目结构->模块->语言级别为8(我的maven项目设置为1.8 java)

AND

File -> Settings -> Build, Execution, Deployment -> Compiler -> Java Compiler -> 8 also there

文件 -> 设置 -> 构建、执行、部署 -> 编译器 -> Java 编译器 -> 8 也在那里

I hope it would be useful

我希望它会很有用

回答by AMS Abu

Got this same error after updating my Idea and I resolved it as follows:

更新我的想法后遇到了同样的错误,我解决了如下问题:

File >> Project Structure... >> Project

文件>>项目结构...>>项目

“Project language level:” was set to “11 - Local variable syntax for lambda parameters” so I changed it to “SDK default (8 - Lambdas, type annotations etc.)” then applied the change and the error was resolved.

“项目语言级别:”设置为“11 - lambda 参数的局部变量语法”,因此我将其更改为“SDK 默认(8 - Lambdas,类型注释等)”,然后应用更改并解决了错误。

回答by Bojan Vukasovic

Just had same error. Problem was that I had empty package-info.java file. As soon as I added package name inside it worked...

只是有同样的错误。问题是我有空的package-info.java file. 一旦我在里面添加了包名,它就起作用了......

回答by tarkos

I Could say I had similar issue. My case: I switched from new version to old version of the project in workspace, try to run single junit which require recompil, recompilation error was thrown with invalid target.

我可以说我有类似的问题。我的情况:我在工作区中从项目的新版本切换到旧版本,尝试运行需要重新编译的单个junit,使用无效目标引发重新编译错误。

From Projects settings (F4) in IntelliJ everything was looking good and java was set to 1.7. But when I try recompile from IDE error was thrown because of wrong target level. After checking I found that in one of the iml file language was set to JDK_11. After manual change to JDK_1_7 everything back to normal.

从 IntelliJ 中的项目设置(F4)来看,一切看起来都不错,java 设置为 1.7。但是当我尝试从 IDE 重新编译时,由于错误的目标级别而引发错误。检查后我发现其中一个 iml 文件语言被设置为 JDK_11。手动更改为 JDK_1_7 后一切恢复正常。

Worth to also manual check lang level in *iml files created by IDE.

值得在 IDE 创建的 *iml 文件中手动检查 lang 级别。

回答by neonidian

If building a project through a build system (Maven, Gradle etc..) works but IntelliJ show Invalid target releaseerror, then do the following,

如果通过构建系统(Maven、Gradle 等)构建项目但 IntelliJ 显示Invalid target release错误,则执行以下操作,

  1. Close IntelliJ

  2. Go to the directory of the project

  3. Delete the .idea/ directory

  4. Start IntelliJ with the project's directory

  1. 关闭 IntelliJ

  2. 进入项目目录

  3. 删除 .idea/ 目录

  4. 使用项目目录启动 IntelliJ

This will re-create the .idea/ directory and will no longer show the error

这将重新创建 .idea/ 目录并且不再显示错误

回答by Shri

For me, I was having the same issue but it was with java v8, I am using a different version of java on my machine for my different projects. While importing one of my project I got the same problem. To check the configuration I checked all my SDK related settings whether it is in File->Project->Project Structure / Modulesor in the Run/Debug configuration setting. Everything I set to java-8but still I was getting the same issue. While checking all of the configuration files I found that compiler.xml in .idea is having an entry for the bytecodeTargetLevel which was set to 11. Here if I change it to 8 even though it shows the same compiler output and removing <bytecodeTargetLevel target="11" />from compiler.xmlresolve the issue.

对我来说,我遇到了同样的问题,但是java v8我在我的机器上为我的不同项目使用了不同版本的 java。在导入我的一个项目时,我遇到了同样的问题。为了检查配置,我检查了所有与 SDK 相关的设置,无论它是在File->Project->Project Structure / Modules运行/调试配置设置中还是在运行/调试配置设置中。我设置的一切,java-8但我仍然遇到同样的问题。在检查所有配置文件时,我发现 .idea 中的 compiler.xml 有一个设置为 11 的 bytecodeTargetLevel 条目。如果我将其更改为 8,即使它显示相同的编译器输出并<bytecodeTargetLevel target="11" />compiler.xml解决问题中删除.

回答by amertkara

There is also the possibility of Maven using a different version of JDK, in that case you can set Maven to use the project default JDK version.

Maven 也有可能使用不同版本的 JDK,在这种情况下,您可以将 Maven 设置为使用项目默认的 JDK 版本。

enter image description here

enter image description here