Idea Intellij: Error:java: error: release version 20 not supported, maven.compiler.target in pom.xml
Asked Answered
W

5

9

I have a Idea Intellij project like this:

Test.java:

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

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>

Idea Intellij

File -> Settings -> Build, Execution, Deployment -> Compiler -> Java Compiler

enter image description here

File -> Project Structure -> Project

enter image description here

File -> Project Structure -> Modules -> Sources

enter image description here

File -> Project Structure -> Modules -> Dependencies

enter image description here

When trying to run Test.java I get an error:

Error:java: error: release version 20 not supported

enter image description here

I use Ubuntu. When I type java --version in the terminal, I get:

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

I'm not sure how I can make Intellij use Java 20

When I use

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

the code runs fine. However I want a newer Java to be used, so that features like enhanced switch blocks:

switch (value)  {
 case 1 -> {}
}

are available

Whitewall answered 9/3, 2023 at 20:18 Comment(3)
Change IDE Project JDK to 20, you are using JDK 11 which cannot target level 20. Also do the same for Maven JDK: i.imgur.com/SbuV0a9.png.Gilson
off topic: please replace the source and target properties with maven.compiler.release to ensure code is compatible when it is being built with a newer JDK.Toombs
For me doing, "File->Project Structure->Project->Language Level" Then selecting the right version fixed the problem.Gammy
W
0

I think my problem was connected to this:

Intellij insists on compiling with javac 11.0.3 instead of 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.

I had

Information:javac 11 was used to compile java sources

message before the actual error message.

So I think despite all the correct settings of JDK, Idea Intellij just insisted on using JDK 11 to compile and there was nothing that could be done about it.

I ended up just downloading the latest Intellij version and now it works fine

Whitewall answered 18/3, 2023 at 0:29 Comment(0)
M
8

You don't need to have Java 20 to use the enhanced switch blocks java 17 has it too. And you are building it in java 11, and compiling in java 20, which is the reason you are getting that error.

Generaly you need to add java 20 to your project in the screenshot file -> Project Structure -> project to use the libraries of java 20 that are compilable in java 20.

I hope that makes sense.

Misjoinder answered 24/3, 2023 at 15:33 Comment(0)
B
3

I would recommend using 15 above, i personally use 17version by amazon correto and that switch feature is available. its up to what new java features you want to use.

however if you want to use the 20 version you had to set the sdk/jdk,language level to 20

like this:

https://i.sstatic.net/SBena.png

https://i.sstatic.net/TbyJh.png

https://i.sstatic.net/BAAQJ.png

https://i.sstatic.net/4tJmI.png

you might be missing those points.

Billiebilling answered 9/3, 2023 at 20:59 Comment(0)
P
1

In Project settings you need to set Project SDK to 20 and project language level to 20

Pragmatic answered 20/3, 2023 at 3:30 Comment(0)
W
0

I think my problem was connected to this:

Intellij insists on compiling with javac 11.0.3 instead of 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.

I had

Information:javac 11 was used to compile java sources

message before the actual error message.

So I think despite all the correct settings of JDK, Idea Intellij just insisted on using JDK 11 to compile and there was nothing that could be done about it.

I ended up just downloading the latest Intellij version and now it works fine

Whitewall answered 18/3, 2023 at 0:29 Comment(0)
K
0

I got the same problem when I change jdk from jdk11 to GrealVM-java11 in intellij and I solve this problem by Changing JDK_X to JDK_11 in .idea/misc.xml.

See also https://youtrack.jetbrains.com/issue/IDEA-240068 .

Kalgoorlie answered 23/5, 2023 at 16:9 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.