Java“此语言级别不支持lambda表达式”

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

Java "lambda expressions not supported at this language level"

javaintellij-idealambdajava-8

提问by Czipperz

I was testing out some new features of Java 8 and copied the example into my IDE (Eclipse originally, then IntelliJ) as shown here

我的Java 8的一些新功能和复制的例子测试伸到我的IDE(Eclipse的原话的IntelliJ)如图所示这里

Eclipse offered no support whatsoever for lambda expressions, and IntelliJ kept reporting an error

Eclipse 不提供对 lambda 表达式的任何支持,并且 IntelliJ 不断报告错误

Lambda expressions not supported at this language level

此语言级别不支持 Lambda 表达式

I would like to know if this is a problem with my install, the code, or support.

我想知道这是否是我的安装、代码或支持方面的问题。

采纳答案by Abhilash Divakaran

In IntelliJ IDEA:

在 IntelliJ IDEA 中:

In File MenuProject StructureProject, change Project Language Levelto 8.0 - Lambdas, type annotations etc.

File MenuProject Structure→ 中Project,更改Project Language Level8.0 - Lambdas, type annotations etc.

For Android 3.0+ Go FileProject StructureModuleappand In Properties Tab set Source Compatibilityand Target Compatibilityto 1.8 (Java 8)

对于 Android 3.0+ 转到FileProject StructureModuleapp并在属性选项卡中设置Source CompatibilityTarget Compatibility转到1.8 (Java 8)

Screenshot:

截屏:

enter image description here

在此处输入图片说明

回答by Stephen C

Actually, Eclipse support for Java 8 is available: see hereand here.

实际上,Eclipse 对 Java 8 的支持是可用的:请参阅此处此处

  • For Kepler, it is available as a "feature patch" against SR2 (i.e. Eclipse 4.3.2)
  • For Luna (Eclipse 4.4) it will be in the standard release. (release schedule)
  • 对于 Kepler,它可以作为针对 SR2(即 Eclipse 4.3.2)的“特性补丁”
  • 对于 Luna (Eclipse 4.4),它将在标准版本中。(发布时间表

回答by Gijs Overvliet

As Stephen C pointed out, Eclipse Kepler (4.3) has Java 8 support when the patch is installed (installation instructions here)

正如 Stephen C 指出的,Eclipse Kepler (4.3) 在安装补丁时支持 Java 8(安装说明在这里

Once installed, you'll need to tell your projects to use java 8. First add the JDK to eclipse:

安装后,您需要告诉您的项目使用 java 8。首先将 JDK 添加到 eclipse:

  • Go to Window -> Preferences
  • Go to Java -> Installed JREs
  • Add Standard VM, and point to the location of the JRE
  • Then go to Compiler
  • Set Compiler compliance level to 1.8
  • 转到窗口 -> 首选项
  • 转到 Java -> 已安装的 JRE
  • 添加标准VM,并指向JRE的位置
  • 然后进入编译器
  • 将编译器合规性级别设置为 1.8

Then tell the project to use JDK 1.8:

然后告诉项目使用JDK 1.8:

  • Go to Project -> preferences
  • Go to Java Compiler
  • Enable project specific settings
  • Set Compiler compliance level to 1.8
  • 转到项目 -> 首选项
  • 转到 Java 编译器
  • 启用项目特定设置
  • 将编译器合规性级别设置为 1.8

回答by prayagupd

For intellij 13, Simply change the Project language levelitself to 8.0with following navigation.

对于 Intellij 13,只需使用以下导航将项目语言级别本身更改为8.0

File
|
|
 ---------Project Structure -> Project tab
          |
          |________Project language level

java8

java8

Module lang level

模块语言级别

I also had to update Modules lang levelwhen there was no maven plugin for java compiler.

Modules lang level当没有 .maven 插件时,我也不得不更新java compiler

File
|
|
 ---------Project Structure -> Modules tab
          |
          |________ language level

Modules lang level

模块语言级别

But this Module lang levelwould automatically be fixed if there's already a maven pluginfor it,

但是Module lang level如果已经有一个maven plugin,这将自动修复,

    <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-compiler-plugin</artifactId>
        <version>3.6.0</version>
        <configuration>
            <source>1.8</source>
            <target>1.8</target>
        </configuration>
    </plugin>

After changes everything looks good

更改后一切看起来都不错

回答by user2682809

You should change source code Language Level also on the Source tab (Modules part).

您还应该在源选项卡(模块部分)上更改源代码语言级别。

Change Language Level

Change Language Level

回答by Daniel de Zwaan

To compile with Java 8 (lambdas etc) in IntelliJ IDEA 15;

在 IntelliJ IDEA 15 中使用 Java 8(lambdas 等)进行编译;

  • CTRL + ALT + SHIFT + Sor File / Project Structure

  • Open Project tab

  • Set Project SDK to compatible java version (1.8)

  • Set Project language level to 8

  • Open Modules tab

  • Set Language level to 8, click OK

  • CTRL + ALT + Sor File / Settings

  • Build, Execution, Deployment / Compiler / Java Compiler

  • Change Target bytecode version to 1.8, click OK

  • CTRL + ALT + SHIFT + S或文件/项目结构

  • 打开项目选项卡

  • 将项目 SDK 设置为兼容的 Java 版本 (1.8)

  • 将项目语言级别设置为 8

  • 打开模块选项卡

  • 将语言级别设置为 8,单击“确定”

  • CTRL + ALT + S或文件/设置

  • 构建、执行、部署/编译器/Java编译器

  • 将目标字节码版本更改为 1.8,单击确定

回答by user5760784

In intellij 14, following settings worked for me. Intellij14 Settings

在 Intellij 14 中,以下设置对我有用。 Intellij14 设置

回答by Dean Jain

Even after applying above defined project specific settings on IntelliJ as well as Eclipse, it was still failing for me !

即使在 IntelliJ 和 Eclipse 上应用了上面定义的项目特定设置后,它对我来说仍然失败!

what worked for me was addition of maven plugin with source and target with 1.8 setting in POM XML:

对我有用的是在POM XML中添加带有源和目标的maven插件,设置为1.8:

<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>2.0.2</version>
            <configuration>
                <source>1.8</source>
                <target>1.8</target>
            </configuration>
        </plugin>

        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-jar-plugin</artifactId>
            <configuration>
                <archive>
                    <manifest>
                        <addClasspath>true</addClasspath>
                        <classpathPrefix>lib/</classpathPrefix>
                        <mainClass>com.abc.sparkcore.JavaWordCount</mainClass>
                    </manifest>
                </archive>
            </configuration>
        </plugin>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-dependency-plugin</artifactId>
            <executions>
                <execution>
                    <id>copy</id>
                    <phase>install</phase>
                    <goals>
                        <goal>copy-dependencies</goal>
                    </goals>
                    <configuration>
<outputDirectory>${project.build.directory}/lib</outputDirectory>
                    </configuration>   
     </execution>
            </executions>
        </plugin>
    </plugins>
</build> 

回答by Toseef Zafar

Ctrl + Alt + Shift + S and then from project select the language level 1.8, read more about Lambda Expressions here

Ctrl + Alt + Shift + S 然后从项目中选择语言级别 1.8,在此处阅读有关 Lambda 表达式的更多信息

回答by kuzevni

Possible Cause #1 (same as the first 3 answers for this question): Project Structure Settings

可能的原因 #1(与此问题的前 3 个答案相同):项目结构设置

  1. File > Project Structure > Under Project Settings, go to Project > set Project SDKto 1.8 (with version 65 or above, latest is 112) and set Project Language Levelto 8.

  2. File > Project Structure > Under Platform Settings, go to Project > ensure that your JDK home pathis set to 1.8 (with version 65 or above, latest is 112).

  1. 文件>项目结构>在项目设置下,进入项目>将项目SDK设置为1.8(65或以上版本,最新为112)并将项目语言级别设置为8。

  2. 文件 > 项目结构 > 在平台设置下,进入项目 > 确保您的JDK 主路径设置为 1.8(65 或更高版本,最新为 112)。

If the above does not work, check the target bytecode versionof your compiler and move on to Possible Cause #2.

如果上述方法不起作用,请检查编译器的目标字节码版本,然后转到可能的原因 #2。

Possible Cause #2: Compiler

可能的原因#2:编译器

  1. File > Settings > Build, Execution, Deployment > Under Compiler, go to Java Compiler > set your Project Bytecode Versionto 1.8, as well as all the Target Bytecode Versionfor your modules. Click Applyand hit OK. Restart your IntelliJ IDE. Lambda expression was accepted in my IDE at this point.
  1. 文件 > 设置 > 构建、执行、部署 > 在编译器下,转到 Java 编译器 > 将您的项目字节码版本以及模块的所有目标字节码版本设置为 1.8 。单击“应用”并单击“确定”。重新启动 IntelliJ IDE。此时我的 IDE 接受了 Lambda 表达式。

Best wishes :)

最好的祝愿 :)