Can't use latest Java Version (JDK 21) in IntelliJ IDEA
Asked Answered
K

4

17

I'm using macOS and I want to try Java 21 in IntelliJ IDEA. I believe I have done all the required steps to change Java version of a project / module. Nevertheless I still can't use new Java 21 Features, such as String Templates. How to be able to use Java 21 in IntelliJ?

enter image description here

I have changed:

  • Project SDK to 21

  • Module SDK to 21

  • OS java version to 21. Typing java -version in terminal outputs:

    java version "21" 2023-09-19 LTS

    Java(TM) SE Runtime Environment (build 21+35-LTS-2513)

    Java HotSpot(TM) 64-Bit Server VM (build 21+35-LTS-2513, mixed mode, sharing)

  • I have also updated IntelliJ to version 2023.2

Kelso answered 20/9, 2023 at 10:2 Comment(2)
Have you set the language level to "21 (Preview)"?Tallbot
Language Level 21 didn't show up because my Intellij was only on version 2023.2. I needed to update it to 2023.2.2 to be able to use Language Level 21Kelso
K
21

I needed to update Intellij version to 2023.2.2 and change Language level (File -> Project structure -> project -> language level) to 21 (preview)

language level 21 (preview) The new language level won't show up until Intellij is updated to the latest version.

To update Intellij, I needed to manually check for new update

Manually updating Intellij on Mac

Intellij IDEA | Check for Updates

enter image description here

Manually updating Intellij on Windows / Linux

File | Settings | Appearance & Behavior | System Settings | Updates

enter image description here

Kelso answered 20/9, 2023 at 11:3 Comment(1)
Yes, see the single sentence about JDK-21 in blog.jetbrains.com/idea/2023/09/intellij-idea-2023-2-2 :-)Kierakieran
T
6

I followed a few options mentioned above, but nothing worked. The main problem was that the Gradle plugin was configured to use an older JDK version. I had to change it in the below settings to use JDK-21. After changing the JDK here, then everything worked fine.

Gradle Settings

Transitive answered 14/3 at 5:54 Comment(0)
S
2

In addition to Nicolas' answer, if you have a Maven project you should also add the build plugin, shown below, to the pom.xml.

<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <configuration>
                <source>21</source>
                <target>21</target>
                <compilerArgs>--enable-preview</compilerArgs>
            </configuration>
        </plugin>
    </plugins>
</build>
Supertanker answered 27/11, 2023 at 17:20 Comment(0)
I
0

At first I installed Oracle jdk from https://www.oracle.com/ae/java/technologies/downloads/ and tried everything above which progressed me to the stage where compile/run/verify was working from Maven command line or plugin but not from IntelliJ ( 2023.3.4 - Community Edition).

At last, re-downloading Oracle OpenJDK 21 from Intelli-J itself fixed them too.

Clicke here to see the image

Iberian answered 18/3 at 10:29 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.