Using Java 21 with IntelliJ IDEA
Published: 2023-11-18 16:55:06

Java 21 was released last September/23 with a lot of new features, such as Virtual Threads, Structured Concurrency (preview), Record Patterns, Sequenced Collections, and many others (if you are interested you can read more on this article). However, how can you use it? Do you only need to update the JDK version of your project? What else you should do to be ready to start rocking some code?
Following you will learn 3 steps to code with Java 21 using IntelliJ as your IDEA in Linux/Ubuntu.

## 0) Download IntelliJ.

For that, you can follow this [article](https://www.jetbrains.com/help/idea/installation-guide.html) or go to the download page.

```
sudo snap install intellij-idea-community --classic
```

## 1) Install locally the JDK 21

The usage of SDKMan helps you to manage different Java versions on your machine in a simple way, even the last one.
https://sdkman.io/usage

After installing the SDKMan, you should run:

```
sdk list java
#choose what java 21 version you want to install, in this example I will use 21.0.1-oracle
sdk install java 21.0.1-oracle
#them you can set the previous version as the default.
sdk default java 21.0.1-oracle
#or only use that on your terminal
sdk use java 21.0.1-oracle
```
Now you can check the Java version in your environment:

```
java -version
#java version "21.0.1" 2023-10-17 LTS
#Java(TM) SE Runtime Environment (build 21.0.1+12-LTS-29)
#Java HotSpot(TM) 64-Bit Server VM (build 21.0.1+12-LTS-29, #mixed mode, sharing)
```

## 2) Add the JDK to your IntelliJ Project

This step is not that simple and requires some attention!

First, you need to go from the top Menu _File->Project Structure_ or use the shortcut [ctrl+alt+shit+s]

Here you will find a window with your project settings, in this window you need to click on the SDK drop-down, and if everything went well with the SDKMan Installation in the previous step, your $JAVA_HOME will detect a new SDK as shown in the following image.

![Detected SDKs based on the $JAVA_HOME config ](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/bw3wa4r7bfqziibqhyep.png)

Choose the Java 21 version that you installed ("21.0.1-oracle"), when choosing a new SDK IntelliJ runs a "Home path validity", don't worry about that, it might take a few minutes to finish!

![Home Path Validity after choosing Java 21 version](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/1plngep8q0h096oj7cpt.png)

To make sure that everything is correct, you can check on the same window the "_Platform Settings-> SDKs_", the chosen version might be there!

![Path to Platform Settings-> SDKs](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/doavcg7xkmnthdam3gbj.png)

Depending on the version of your IntelliJ IDEA you will need to specify the Language Level since not all versions are compatible with the new features of Java 21.
On the same window (Project Structure) choose "X - Experimental Features"

![Choosing Experimental Features in IntelliJ](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/19jkp3d62kzoiktzsvq3.png)


## Trick Step) Using Experimental Features in IntelliJ

Since we still need to use the mode of "Experimental Features, IntelliJ has a trick to make it work well: the _"--enable-preview"_

To do so, go to _Settings [ctrl+alt+s] -> Build, Execution, Deployment -> Compiler -> Java Compiler_.


![Java Compiler Menu to configure it](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/i0iut5y5k3k26fikmzqg.png)

In the section "Override compiler parameters per module" include a new configuration!

![Overriding compiler parameters](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/8rv25ub5t9gtd63xd3ea.png)

The configuration might appear like this in the end:

![new variable for the module](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/b6hdk7vdh6ktjwjv3jkn.png)

## Let's code!

With everything configured, it's time to brave the new features of Java 21 release! So here are some amazing contents that might be helpful for that:

For this, I would recommend the following articles:
[The Evolution of Java: Challenging Stereotypes and Embracing Modernity](https://dzone.com/articles/the-evolution-of-java-challenging-stereotypes-and) by [Bazlur](https://www.linkedin.com/in/bazlur/)

[Exploring Java 21's Intriguing Additions (Part 1) ](https://dzone.com/articles/exploring-java-21s-intriguing-additions-part-1) by [Bazlur](https://www.linkedin.com/in/bazlur/)

[Unlocking Performance: Exploring Java 21 Virtual Threads](https://www.youtube.com/watch?v=3uAms_OJ2Rs) by [Otavio Santana](https://twitter.com/otaviojava)

[Suas maiores dúvidas sobre Java no Backend | Esquenta TDC Business](https://www.youtube.com/watch?v=vrFEhtGj1GY) with [Gabriel Silva](https://twitter.com/GabrielS_Java) and [Elder Moraes](https://twitter.com/elderjava)

[Java 21 is here: Virtual threads, string templates, and more](https://blogs.oracle.com/javamagazine/post/java-21-now-available)