Idea Intellij:错误:java:错误:不支持发布版本20,pom.xml中的maven.compiler.target

par*_*cer 7 java intellij-idea maven adoptopenjdk java-19

我有一个Idea Intellij这样的项目:

测试.java

public class Test {
    public static void main(String[] args) {
        System.out.println("Test");
    }
}
Run Code Online (Sandbox Code Playgroud)

pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>test</groupId>
    <artifactId>test</artifactId>
    <version>1.0-SNAPSHOT</version>
     
    <properties>
        <maven.compiler.target>20</maven.compiler.target>
        <maven.compiler.source>20</maven.compiler.source>
    </properties>
</project>
Run Code Online (Sandbox Code Playgroud)

智能理念

File -> Settings -> Build, Execution, Deployment -> Compiler -> Java Compiler
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

File -> Project Structure -> Project
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

File -> Project Structure -> Modules -> Sources
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

File -> Project Structure -> Modules -> Dependencies
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

尝试运行时Test.java出现错误:

Error:java: error: release version 20 not supported
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

我用的是Ubuntu。当我java --version在终端中输入时,我得到:

openjdk 20 2023-03-21
OpenJDK Runtime Environment (build 20+36-2344)
OpenJDK 64-Bit Server VM (build 20+36-2344, mixed mode, sharing)
Run Code Online (Sandbox Code Playgroud)

我不知道如何让 Intellij 使用 Java 20

当我使用

   <properties>
        <maven.compiler.target>11</maven.compiler.target>
        <maven.compiler.source>11</maven.compiler.source>
    </properties>
Run Code Online (Sandbox Code Playgroud)

代码运行良好。不过,我希望使用更新的 Java,以便具有以下功能enhanced switch blocks

switch (value)  {
 case 1 -> {}
}
Run Code Online (Sandbox Code Playgroud)

可用

小智 7

你不需要有 Java 20 来使用enhanced switch blocksjava 17 也有它。您在 java 11 中构建它,并在 java 20 中编译,这就是您收到该错误的原因。

通常,您需要将 java 20 添加到屏幕截图中的项目中file -> Project Structure -> project,才能使用可在 java 20 中编译的 java 20 库。

我希望这是有道理的。


par*_*cer 0

我认为我的问题与此有关:

Intellij 坚持使用 javac 11.0.3 而不是 Java 7 进行编译

IntelliJ IDEA checks all JDKs that are used in the project: the JDKs that are defined on both the project and module levels.

IntelliJ IDEA calculates the latest of these JDKs. This is necessary to make sure that all modules can be compiled.

If the version of the latest JDK configured is lower than 1.6, IntelliJ IDEA will pick the JDK version that is used for running the IDE. This limitation is related to the fact that the compiler API used by IntelliJ IDEA for building projects is supported starting from JDK 1.6.
Run Code Online (Sandbox Code Playgroud)

我有

Information:javac 11 was used to compile java sources
Run Code Online (Sandbox Code Playgroud)

实际错误消息之前的消息。

所以我认为尽管JDK的设置都正确,但Idea Intellij只是坚持使用JDK 11来编译,对此没有什么办法。

我最终只是下载了最新的 Intellij 版本,现在它工作正常