Reputation: 773
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?
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
Upvotes: 20
Views: 48156
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
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
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.
Upvotes: 0
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.
Upvotes: 6
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
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)
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
Manually updating Intellij on Windows / Linux
File | Settings | Appearance & Behavior | System Settings | Updates
Upvotes: 23