使用 IntelliJ IDEA 进行编译的时候提示 Java 的支持 Level 不够

错误提示的信息如下:

By default, the Java language level is set to 5 which is not supported by the current Java version. Update the language level to 6+.Update source level in codebank



问题和原因

这是因为很有可能在你的 maven 配置 POM 的时候的编译插件 maven-compiler-plugin 的版本。

通常这个插件的默认版本都比较低。

如果你使用的是 3.7.0 的话,那么默认使用的是 Java 1.5 的 Level。

如果你修改使用最新的 3.8.1 的话,那么将会默认使用 1.8 的 Level。

目前,我们应该会使用 JDK 8 或者 11,因此你需要手动进行一下设置。

设置有 2 个方法。

第一个方法就是添加下面的参数到属性中。

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

第二个方法就是直接指派到插件的 config 配置中。

如下面的配置参数。

<plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-compiler-plugin</artifactId>
        <version>3.8.1</version>
        <configuration>
          <source>1.8</source>
          <target>1.8</target>
        </configuration>
      </plugin>

JDK 9 及其后续版本

maven-compiler-plugin 插件的 3.6 后续版本开始,添加了一个 release 的配置。

如果你使用 JDK 11 的话,你可以使用下面的配置。

<plugin>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.8.1</version>
                <configuration>
                    <release>11</release>
                </configuration>
            </plugin>

使用上面的配置结果将会能够更好的适配 JDK 11 版本。

了解更多
举报
评论 0