Java 此语言级别不支持 intellij 功能 (...)。我无法编译

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

intellij feature (...) not supported at this language level. I can't compile

javaintellij-ideaforeachjava-8java-7

提问by Seth G.

IntelliJ is showing me this error

IntelliJ 向我展示了这个错误

I just built my new computer and none of my java stuff is working... My regular Intellij was working when I first tried it, but my android studio would't install because it couldn't find the path to the jdk. Hereis the question that I asked about that. Since that wasn't working, I had to reinstall java multiple times, and now my IntelliJ isn't working. Here is a picture of the error (I am getting an error that is telling me the for-each loops aren't supported at this language level).

我刚刚构建了我的新计算机,我的 java 东西都没有工作......当我第一次尝试时,我的常规 Intellij 正在工作,但是我的 android studio 无法安装,因为它找不到 jdk 的路径。是我问的问题。由于那不起作用,我不得不多次重新安装 java,现在我的 IntelliJ 不起作用。这是错误的图片(我收到一个错误,告诉我在此语言级别不支持 for-each 循环)。

I have tried to install the JDK on my D: drive and that didn't work. I am currently trying to use java 1.8u71 (have tried u65 and 1.7u47) except none of those have worked.

我试图在我的 D: 驱动器上安装 JDK,但没有奏效。我目前正在尝试使用 java 1.8u71(已尝试过 u65 和 1.7u47),但这些都没有奏效。

采纳答案by activedecay

IntelliJ is showing me this error

IntelliJ 向我展示了这个错误

are not supported at this language level

此语言级别不支持

Matching the SDK/JDK with Project language level

将 SDK/JDK 与项目语言级别匹配

To activate Java language features in the editor, you need to change your module's Project Structureor Module Settingsto ensure your Project SDKmatches the Project language level.

要在编辑器中激活 Java 语言功能,您需要更改模块的项目结构模块设置,以确保您的项目 SDK项目语言级别相匹配。

Project SDK (JDK) Java 1.5 matches project language level 5 ...

项目 SDK (JDK) Java 1.5 匹配项目语言级别 5 ...

Java 1.6 matches language level 6 ...

Java 1.6 匹配语言级别 6 ...

Java 1.7 matches language level 7, and so on.

Java 1.7 匹配语言级别 7,依此类推。

Example

例子

Depending on your IntelliJ version, hit F4or Ctrl + Alt + Shift - Son your module in the Project pane, or hit Ctrl + Shift - Aand type either "Project Structure" or "Module Settings" to open the Project Structure dialog.

根据您的 IntelliJ 版本,点击F4Ctrl + Alt + Shift - S在项目窗格中的模块上,或者点击Ctrl + Shift - A并键入“项目结构”或“模块设置”以打开项目结构对话框。

Under Project Settingssection, click on the first item in the list, Project.

Project Settings部分下,单击列表中的第一项Project

In the screenshot below, the Project SDK 1.8 (java version '1.8.0_72')matches the selected language level SDK Default (8 - Lambdas, type annotations etc.)because Java 1.8 matches SDK level 8. This activates the Java 8 language features for use in the editor.

在下面的屏幕截图中,项目 SDK1.8 (java version '1.8.0_72')匹配所选语言级别,SDK Default (8 - Lambdas, type annotations etc.)因为 Java 1.8 匹配 SDK 级别 8。这会激活 Java 8 语言功能以在编辑器中使用。

project structure

项目结构

If you don't have the JDK set up under Project SDKthat is a different problem. Solve that one first by clicking Newand adding a JDK.

如果您没有在其下设置 JDK Project SDK,则是另一个问题。首先通过单击New并添加 JDK来解决该问题。

回答by HankCa

I had this same problem and none of the solutions I have found worked.

我遇到了同样的问题,但我发现的所有解决方案都没有奏效。

I was using Maven projects and so decided to try running a maven install. It too said:

我正在使用 Maven 项目,因此决定尝试运行maven install. 它也说:

[ERROR]  thefile.java:[24,77] diamond operator is not supported in -source 1.5
[ERROR] (use -source 7 or higher to enable diamond operator)
[ERROR] theFile.java:[91,62] lambda expressions are not supported in -source 1.5
[ERROR] (use -source 8 or higher to enable lambda expressions)

So I added the properties to the Maven build pom.xmlfile:

所以我将属性添加到 Maven 构建pom.xml文件中:

<properties>
    <spring.version>4.1.6.RELEASE</spring.version>
    <java.version>1.8</java.version>
    <maven.compiler.source>${java.version}</maven.compiler.source>
    <maven.compiler.target>${java.version}</maven.compiler.target>
</properties>

And refreshed the Maven project in IntelliJ. It worked!

并在 IntelliJ 中刷新了 Maven 项目。有效!

回答by melston

Another issue I had that caused this error in a Gradle project was the sourceCompatibilitysetting in the build.gradle file. This must also match the value you are expecting to need. The default value that I have seen IntelliJ use when creating a new Gradle project is 1.5. If you are using lambdas, for instance, this will cause the build to fail.

在 Gradle 项目中导致此错误的另一个问题是sourceCompatibilitybuild.gradle 文件中的设置。这也必须与您期望需要的值相匹配。我在创建新 Gradle 项目时看到 IntelliJ 使用的默认值是1.5. 例如,如果您使用 lambda,这将导致构建失败。

回答by ramtech

When I had multiple independent modules in the project, I had to do below setting in addition to @activedecay's answer: Under Project Settings, click on Modules. Select specific module. Select appropriate Language Level.

当我在项目中有多个独立模块时,除了@activedecay 的回答之外,我还必须进行以下设置:在 下Project Settings,单击Modules。选择特定模块。选择合适的Language Level

Screenshot

截屏

回答by kranti

I also had the same problem.

我也有同样的问题。

While I was accessing a static method of an interface through the name of the interface, I got the error saying

当我通过接口名称访问接口的静态方法时,我收到错误消息

static interface method invocations are not supported at this language level 5

此语言级别 5 不支持静态接口方法调用

As this is Java 1.8, I have checked some things:

由于这是 Java 1.8,我检查了一些内容:

  1. I am using Intellij. So by doing CTRL+ALT+SHIFT+SProject Structure dialog box opens, go to

    Project -> Project language level

    set this to SDK default (8-Lambdas,type annotations etc).

  2. In Project structure open

    Modules -> Sources -> Language level

    set this to Project default (8-Lambdas,type annotations etc).

  3. Apply-> OK.

  1. 我正在使用 Intellij。所以通过做CTRL+ ALT+ SHIFT+S项目结构对话框打开,转到

    项目 -> 项目语言级别

    将此设置为 SDK 默认值(8-Lambdas、类型注释等)。

  2. 在项目结构中打开

    模块 -> 来源 -> 语言级别

    将此设置为项目默认值(8-Lambdas,类型注释等)。

  3. 申请-> 确定。

Then the error will go away.

然后错误就会消失。

回答by Solution Spirit

Let me tell the solution in one image.

让我用一张图片告诉解决方案。

  • File -> Project Structure -> Modules -> Properties tab -> Source Compatibility (Java 8)
  • 文件 -> 项目结构 -> 模块 -> 属性选项卡 -> 源代码兼容性(Java 8)

enter image description here

在此处输入图片说明