Nicolas Othmar
Nicolas Othmar

Reputation: 773

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

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:

Upvotes: 20

Views: 48156

Answers (6)

Vipresh
Vipresh

Reputation: 1317

I faced similar issue, I had to go and un-check "Use '--release' option for cross-compilation (java 9 and later) under File > Settings > Build, Execution, Deployment > Compiler > Java Compiler

Upvotes: 1

Zehra Bilgin
Zehra Bilgin

Reputation: 1

I updated the intellij idea to IntelliJ IDEA 2023.1.7 (Ultimate Edition) and reload my maven project. After that i check the Project Structure->Project Settings->Modules tab and see the modules language level is upgraded to X-Experimental feautures, and my project can be compiled.

Upvotes: 0

cihan uluisik
cihan uluisik

Reputation: 1

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

Upvotes: 0

Tejash JL
Tejash JL

Reputation: 396

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

Upvotes: 6

Toni
Toni

Reputation: 5165

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>

Upvotes: 3

Nicolas Othmar
Nicolas Othmar

Reputation: 773

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

Upvotes: 23

Related Questions