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

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


当前回答

你应该用下面的方法做更多的改变:


1通过IntelliJ GUI

正如“tataelm”所提到的:

项目结构>项目设置>模块>语言级别:>然后更改为您喜欢的语言级别


2直接编辑IntelliJ配置文件

打开<ProjectName>. exe文件。iml文件(如果你使用IntelliJ,它会自动创建在你的项目文件夹中)直接通过编辑下面的行,

<组件名称="NewModuleRootManager" LANGUAGE_LEVEL="JDK_1_5">

到:<组件名称="NewModuleRootManager" LANGUAGE_LEVEL="JDK_11">

因为你的方法也意味着编辑这个文件。:)


方法1实际上是要求IntelliJ帮助编辑.iml文件,而不是由您直接编辑。

其他回答

在IntelliJ中,缺省的maven编译器版本小于版本5,这是不受支持的,因此我们必须手动更改maven编译器的版本。

我们有两种定义版本的方法。

第一个方法:

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

第二种方式:

<build>
  <plugins>
    <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-compiler-plugin</artifactId>
        <version>3.8.0</version>
        <configuration>
            <source>8</source>
            <target>8</target>
        </configuration>
    </plugin>
  </plugins>
</build>

见https://stackoverflow.com/a/12900859/104891。

首先,像这样在pom.xml中设置语言级别/发布版本:

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

否则,Maven将默认值设置为1.5。你还需要包含maven-compiler-plugin(如果你还没有的话):

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

此外,尝试在以下每个地方更改Java版本:

11.文件->项目结构->项目->项目SDK ->

11.文件->项目结构->项目->项目语言级别->

11 .文件->项目结构->项目->模块-> ->来源->

11 . In project -> ctrl + alt + s -> Build, Execution, Deployment -> Compiler -> Java Compiler -> project bytecode version ->

在项目-> ctrl + alt + s ->构建,执行,部署->编译器-> Java编译器->模块-> 1.11。

2023年更新:

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

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

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

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

我做了上面所有的事情,但必须在IntelliJ中再做一件事:

>模块

我不得不改变每个模块的“语言级别”

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