gpt4 book ai didi

java - 使用 lambda 表达式编译代码时出错

转载 作者:塔克拉玛干 更新时间:2023-11-03 05:08:48 25 4
gpt4 key购买 nike

我有以下代码:

package com.mongoDB;

import spark.Spark;

public class HelloWorldSparkStyle {
public static void main(String[] args) {
Spark.get("/hello", (req, res) -> "Hello World");
}
}

当我通过 main 方法运行它时它运行良好,但当我尝试编译它时抛出以下错误:

\HelloWorldSparkStyle.java:[9,33] error: lambda expressions are not supported in -source 1.5

D:\WorkspaceWithJava8\BeginnerProject>javac -version
javac 1.8.0_60

我正在使用 Eclipse IDE 并尝试通过命令行编译它。

最佳答案

默认情况下,maven-compiler-plugin 使用 Java 5 编译类。引用 its documentation :

Also note that at present the default source setting is 1.5 and the default target setting is 1.5, independently of the JDK you run Maven with. If you want to change these defaults, you should set source and target as described in Setting the -source and -target of the Java Compiler.

您需要将其配置为使用 Java 8,如下所示:

<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.3</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>

关于java - 使用 lambda 表达式编译代码时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33342713/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com