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

by
Maya Patel
androidsdkmanager intellij-idea java-21

Quick Fix: To use JDK 21 in Intellij IDEA, update Intellij to version 2023.2.2 and set the language level to 21 (preview) in Project structure. If the language level 21 option is not visible, manually check for updates in Intellij.

The Problem:

I’m using macOS and want to leverage Java 21 in IntelliJ IDEA, but despite changing the Java version for the project, module, and OS, I am unable to utilize the new features of Java 21, such as String Templates. Although I have ensured the project SDK, module SDK, and OS Java version are set to 21, I still face this issue. How can I enable the use of Java 21 in IntelliJ IDEA?

The Solutions:

Solution 1: Update IntelliJ IDEA to 2023.2.2 and Set Language Level to 21 (Preview)

To use Java 21 features in IntelliJ IDEA on macOS:

  1. Update IntelliJ IDEA to version 2023.2.2:
    • On Mac: Click “IntelliJ IDEA” in the menu bar and select “Check for Updates”.
    • On Windows/Linux: Go to “File” > “Settings” > “Appearance & Behavior” > “System Settings” > “Updates”.
  2. Change the Language Level to 21 (preview):
    • Go to “File” > “Project Structure” > “Project”.
    • Under “Project SDK”, select Java 21.
    • Under “Language Level”, select “21 (preview)”.
    • Click “Apply” and then “OK”.

After following these steps, you should be able to use Java 21 features in IntelliJ IDEA.

Solution 2: Update Module SDK

If you have already completed the necessary steps to change the Java version of your project/module in Intellij IDEA but are still unable to use new Java 21 features, try the following:

  1. Right-click on the module in your project and select “Open Module Settings” from the context menu.
  2. In the “Module Settings” dialog box, navigate to the “SDKs” tab.
  3. Click the “Add” button and select the JDK 21 installation that you want to use for the module.
  4. If you don’t have JDK 21 installed, you can download it from the Oracle website or use the “Download JDK” option in the “Add SDK” dialog box.
  5. Once you have added JDK 21 to the list of available SDKs, select it as the “Project SDK” and “Module SDK”.
  6. Click “Apply” and then “OK” to save your changes.

This should enable you to use Java 21 features in your project.

Solution 3: Configure the compiler settings

If you have a Maven project, you can configure the compiler settings to use Java 21 by adding the following code to your pom.xml file:

<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>

This will instruct Maven to use Java 21 as the source and target version for compilation, and to enable preview features (which are required for String templates).

Q&A

How to be able to use Java 21 in Intellij?

Update Intellij to 2023.2.2 and change Language level to 21 (preview).

What steps can be taken to add a new JDK in Intellij?

Right click on the module, open Module Settings, choose SDKs, Add JDK.

How to add build plugin in Maven project to enable Java 21?

Add the build plugin to the pom.xml with source, target and compilerArgs configured.

Video Explanation:

The following video, titled "CAN - Vitamin C (Official Audio) - YouTube", provides additional insights and in-depth exploration related to the topics discussed in this post.

Play video

Can 'Vitamin C' — taken from the classic album 'Ege Bamyasi'. Listen to/Buy 'Ege Bamyasi' here: https://mute.ffm.to/nknonoq.