IntelliJ:错误:java:错误:不支持发行版本5


14

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

Error:java: error: release version 5 not supported

java --version通过终端运行,我得到以下输出:

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通过终端运行,我得到以下输出:

javac 11.0.5

要在Java编译器的设置(如建议在这里)我看到这一点:

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.

您认为出了什么问题?我该如何解决?


项目和模块的语言级别是什么?
Bas Leijdekkers

Answers:


33

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

首先,设置language level/ release versionspom.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版本:

文件->项目结构->项目-> Project SDK-> 11。

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

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

在项目中-> ctrl+ alt+ s->构建,执行,部署->编译器-> Java编译器->项目字节码版本-> 11

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


2
我已经完成了所有工作,但是无论何时运行应用程序,我仍然会看到相同的错误
Tushar Jajodia

@TusharJajodia尝试包括maven-compiler-plugin作为依赖
乔什·约翰逊

在网上尝试各种解决方案而没有运气之后,这条特殊的线解决了这个问题:文件->项目结构->项目->模块->->源-> 11
Maude

2

花了我一段时间来汇总一个实际的解决方案,但是这里是摆脱这种编译错误的方法。

  1. 打开Intelij首选项
  2. 搜索“编译器(或类似“ compi”的东西)
  3. 向下滚动到Maven-> java编译器。在右侧窗格中,将显示模块及其关联的Java编译版本“目标字节码版本”的列表。
  4. 选择> 1.5的版本。如果jdk不可用,则可能需要升级。在此处输入图片说明

1

如果您将Spring Boot用作父级,则应设置java.version属性,因为这将自动设置正确的版本。

<properties>
   <java.version>11</java.version>
</properties>

您自己的项目中定义的属性将覆盖父pom中设置的任何内容。这将覆盖所有必需的属性以编译为正确的版本。

一些信息可以在这里找到:https : //www.baeldung.com/maven-java-version


我现在也开始学习有关Spring Boot的信息,所以这些信息对我来说真的非常有用:)您能详细解释一下吗?(我应该在哪个文件中更改这些属性,我应该写什么...?)谢谢!
罗布

1
我在帖子中添加了一些额外的信息。
V Jansen


0

我将下一个代码添加到我的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>

0

您只需在pom.xml中添加两行,然后问题就消失了。在pom.xml中添加这两行-

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

0

在IntelliJ中,打开pom.xml文件

之前添加此部分(如果文件已包含一个部分,只需将以下行添加到该现有部分中):

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

0

就我而言,唯一可行的解​​决方案是将以下代码块添加到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>
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.