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

时间:2020-10-04 13:27:09

标签: java maven

当使用Eclipse和Maven构建包含不和谐bot的一些简单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]

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

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();

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

使用了lambda,但它们绝对正确,因此此错误并非源于代码中的错误。我该如何解决?

1 个答案:

答案 0 :(得分:0)

嗯,问题出在我为项目配置的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>

...

适应pom.xml可修复此错误。我以为我会在此发布此修复程序,因为我没有发现任何可用于在线解决此特定错误的东西,所以我想我也可以将自己的付费知识添加到互联网上(我花了大约2个小时才发现这是偶然的,但我也不是很有经验。)