编译失败:-source 7 不支持 lambda 表达式

mon*_*ona 2 java maven

当使用 Eclipse 和 Maven 构建一个包含一些用于不和谐机器人的简单 lambda 的项目时,就会发生这种情况。

[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] Total time:  5.330 s
[INFO] Finished at: 2020-10-04T15:14:13+02:00
[INFO] ------------------------------------------------------------------------
[ERROR] Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.8.0:compile (default-compile) on project XXX: Compilation failure: Compilation failure: 
[ERROR] /C:/XXX/listener/TextMessageListener.java:[24,54] lambda expressions are not supported in -source 7
[ERROR]   (use -source 8 or higher to enable lambda expressions)
[ERROR] /C:/XXX/Main.java:[27,30] lambda expressions are not supported in -source 7
[ERROR]   (use -source 8 or higher to enable lambda expressions)
[ERROR] /C:/XXX/Main.java:[33,38] method references are not supported in -source 7
[ERROR]   (use -source 8 or higher to enable method references)
[ERROR] -> [Help 1]
[ERROR]
Run Code Online (Sandbox Code Playgroud)

错误消息中给定位置的代码是例如

client.getEventDispatcher().on(MessageCreateEvent.class)
            .map(MessageCreateEvent::getMessage)
            .filter(message -> message.getAuthor().map(user -> !user.isBot()).orElse(false))
            .flatMap(Message::getChannel)
            .flatMap(channel -> onIncomingMessage(channel))
            .subscribe();
Run Code Online (Sandbox Code Playgroud)

Run as... -> Maven install我尝试在 Eclipse Run 菜单中执行。

使用了 lambda,但它们绝对是正确的,因此这个错误不是来自代码中的错误。我怎样才能解决这个问题?

mon*_*ona 6

嗯,问题出在我的项目的maven配置上。这些行必须进行调整:

...

  <version>0.0.1-SNAPSHOT</version>

  <name>bot</name>
  <url>mywebsite.site</url>

  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <maven.compiler.source>1.7</maven.compiler.source> <---- to 1.8
    <maven.compiler.target>1.7</maven.compiler.target> <---- to 1.8
  </properties>

  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>4.11</version>

...
Run Code Online (Sandbox Code Playgroud)

调整 pom.xml 修复了此错误。我想我应该在这里发布这个修复程序,因为我在网上没有找到任何对这个特定错误有用的东西,所以我想我不妨将我辛苦付出的知识添加到互联网上(我花了大约2个小时,直到我发现这是偶然的,但我也不是很有经验)。