使用lambda表达式编译代码时出错 [英] Error in compilation of code with lambda expression

查看:1374
本文介绍了使用lambda表达式编译代码时出错的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下代码:

package com.mongoDB;

import spark.Spark;

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

当我通过main方法运行它时运行正常但是当我尝试编译它时抛出以下错误:

It runs fine when I run it through main method but throws the following error when I try to compile it:

\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并尝试通过命令行编译它。

I am using Eclipse IDE and trying to compile it through command line.

推荐答案

默认情况下, maven-compiler-plugin 使用Java 5编译类。引用其文档

By default, the maven-compiler-plugin uses Java 5 to compile the classes. Quoting its documentation:


另请注意,目前默认的源设置为1.5,默认目标设置为1.5,与您运行Maven的JDK无关。如果要更改这些默认值,则应设置源代码和目标,如设置Java编译器的-source和-target中所述。

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,如下所示:

You need to configure it to use Java 8, like this:

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

这篇关于使用lambda表达式编译代码时出错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆