我使用的是IntelliJ IDEA Ultimate 2019.3.1。每当我尝试启动任何简单的Java Maven项目(可能它甚至是一个简单的Hello World),我得到以下错误:

Error:java: error: release version 5 not supported

运行java——version by terminal得到如下输出:

openjdk 11.0.5 2019-10-15
OpenJDK Runtime Environment (build 11.0.5+10-post-Ubuntu-0ubuntu1.1)
OpenJDK 64-Bit Server VM (build 11.0.5+10-post-Ubuntu-0ubuntu1.1, mixed mode, sharing)

运行javac——version by terminal得到如下输出:

javac 11.0.5

转到Java编译器的设置(在这里建议),我看到这个:

我尝试将“目标字节码版本”编辑为1.8,但我得到以下错误:

Error:(1, 26) java: package javafx.application does not exist
Error:(2, 20) java: package javafx.stage does not exist
Error:(4, 27) java: cannot find symbol
  symbol: class Application
Error:(12, 23) java: cannot find symbol
  symbol:   class Stage
  location: class Main
Error:(7, 9) java: cannot find symbol
  symbol:   method launch(java.lang.String[])
  location: class Main
Error:(11, 5) java: method does not override or implement a method from a supertype

将其更改为1.11版本,我得到这个错误:

Error:java: Source option 5 is no longer supported. Use 6 or later.

你认为是什么问题?我该怎么解决呢?


当前回答

这个问题的解决方案已经在一些答案中定义了。但还是需要表达一个细节。这个问题完全与maven有关,解决方案也在pom.xml配置中。IntelliJ只读取maven配置,如果配置有错误的信息,IntelliJ也会尝试使用该配置。

如大多数答案所示,要解决这个问题,只需将以下属性添加到项目的pom.xml中。

<properties> 
      <maven.compiler.source>1.8</maven.compiler.source> 
      <maven.compiler.target>1.8</maven.compiler.target> 
</properties>

之后,在IntelliJ端,重新导入maven配置。要重新导入maven配置,右键单击项目的pom.xml文件并选择reimport。IntelliJ首选项中的编译器版本相关信息将根据pom.xml中的值自动更新。

其他回答

在我的情况下,唯一可行的解决方案是将以下块添加到pom.xml:

<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>3.8.0</version> <configuration> <release>12</release>
        </configuration>
        </plugin>
    </plugins>
</build>

这个问题的解决方案已经在一些答案中定义了。但还是需要表达一个细节。这个问题完全与maven有关,解决方案也在pom.xml配置中。IntelliJ只读取maven配置,如果配置有错误的信息,IntelliJ也会尝试使用该配置。

如大多数答案所示,要解决这个问题,只需将以下属性添加到项目的pom.xml中。

<properties> 
      <maven.compiler.source>1.8</maven.compiler.source> 
      <maven.compiler.target>1.8</maven.compiler.target> 
</properties>

之后,在IntelliJ端,重新导入maven配置。要重新导入maven配置,右键单击项目的pom.xml文件并选择reimport。IntelliJ首选项中的编译器版本相关信息将根据pom.xml中的值自动更新。

For me in Intelij click on File -> Project Structure -> select the modules in language level select 11 - Local variable syntax for lambda parameters, click em aply.

Run in terminal mvn clean install.

它解决了我的问题。

2023年更新:

请检查您的pom.xml文件,以了解您配置的java版本。

<properties>
    <java.version>17</java.version>
</properties>

然后下一件事是下载你已经配置的版本的jdk。按照下面的步骤来做。

点击文件->项目结构如下所示 参见下图添加或选择Project SDK 3、如果您的项目没有在列表中,您可以通过点击Add SDK—> download JDK下载特定版本的项目。 完成下载和应用,然后单击确定重新加载应用程序。

在IntelliJ中,打开pom.xml文件。

将这个部分添加到<dependencies>之前(如果你的文件已经有了<properties>部分,只需将下面的<maven.compiler…>行添加到现有的部分):

<properties> 
   <maven.compiler.source>1.8</maven.compiler.source> 
   <maven.compiler.target>1.8</maven.compiler.target> 
</properties>