簡體   English   中英

編譯失敗:-source 7 中不支持 lambda 表達式

[英]Compilation failure: lambda expressions are not supported in -source 7

當使用 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]

錯誤消息中給定位置的代碼是例如

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 菜單中執行Run as... -> Maven install

使用了 lambdas,但它們絕對是正確的,所以這個錯誤不是來自代碼中的錯誤。 我怎樣才能解決這個問題?

好吧,問題在於我為我的項目配置了 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 個小時直到我發現這是偶然的,但我也不是很有經驗)。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM