我使用的是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.

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


当前回答

我将以下代码添加到pom.xml文件中。它解决了我的问题。

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

其他回答

您需要设置语言级别,发布版本,并将maven编译器插件添加到pom.xml

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

<dependency>
  <groupId>org.apache.maven.plugins</groupId>
  <artifactId>maven-compiler-plugin</artifactId>
  <version>3.8.1</version>
</dependency>

我已经用Java编译器和字节码完成了您建议的大部分工作,并在过去解决了这个问题。 我运行Java代码已经将近一个月了(那时一切都已修复),但现在问题又出现了。 不知道为什么,不过挺烦的!

这次的解决方案是: 右键单击项目名称->打开模块设置F4 ->语言级别 ...在那里你可以定义你的项目/pc的语言级别。

无论是过去还是现在,maven/pom配置都不适合我,我已经将Java编译器和字节码设置为12。

在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>
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.

它解决了我的问题。

您只需在pom.xml中添加这两行。在那之后,你的问题就解决了。

<!--pom.xml-->
<properties>
    <maven.compiler.source>1.8</maven.compiler.source>
    <maven.compiler.target>1.8</maven.compiler.target>
</properties>