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 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.
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!
To make sure that everything is correct, you can check on the same window the "Platform Settings-> SDKs", the chosen version might be there!
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"
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.
In the section "Override compiler parameters per module" include a new configuration!
The configuration might appear like this in the end:
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 by Bazlur
Exploring Java 21's Intriguing Additions (Part 1) by Bazlur
Unlocking Performance: Exploring Java 21 Virtual Threads by Otavio Santana
Suas maiores dúvidas sobre Java no Backend | Esquenta TDC Business with Gabriel Silva and Elder Moraes
Java 21 is here: Virtual threads, string templates, and more
Top comments (1)
Thank you for sharing