The most frequently used object-oriented programming language is Java. Java is popular and frequently utilized because of its security features. Many Java aficionados utilize it for various purposes. We can create a wide range of apps using Java, including corporate applications, network applications, desktop applications, online applications, and many others.
Uses of Java Programming Language
The following apps may be created with Java technology:
- Web-based Applications
- Gaming Applications
- Big Data Technologies
- Distributed Applications
- IoT Applications
- Mobile App Development
- Desktop GUI Applications
- Cloud-based Applications
In this tutorial, we are going to cover how to Install Java 21 (OpenJDK 21) on Amazon Linux 2023. Oracle released Java 21 LTS on September 19, 2023, which is the most recent long-term support update for the Java SE platform. Under the Oracle No-Fee Terms and Conditions License, JDK 21 binaries are free to use in production and redistribute at no cost.
New Features in Java 21
Java 21 have the following features:
- String Templates – String templates complement Java’s existing string literals and text blocks by coupling literal text with embedded expressions and template processors to produce specialized results.
- Sequenced Collections – Each such collection has a well-defined first element, second element, and so forth, up to the last element. It also provides uniform APIs for accessing its first and last elements, and for processing its elements in reverse order.
- Record Patterns – Record patterns and type patterns can be nested to enable a powerful, declarative, and composable form of data navigation and processing.
- Foreign Function & Memory API – By efficiently invoking foreign functions (i.e., code outside the JVM), and by safely accessing foreign memory (i.e., memory not managed by the JVM), the API enables Java programs to call native libraries and process native data without the brittleness and danger of JNI.
- Virtual Threads – Virtual threads are lightweight threads that dramatically reduce the effort of writing, maintaining, and observing high-throughput concurrent applications.
- Unnamed Patterns and Variables – Enhance the Java language with unnamed patterns, which match a record component without stating the component’s name or type, and unnamed variables, which can be initialized but not used.
- Unnamed Classes and Instance Main Methods – Far from using a separate dialect of Java, students can write streamlined declarations for single-class programs and then seamlessly expand their programs to use more advanced features as their skills grow.
- Key Encapsulation Mechanism API – Introduce an API for key encapsulation mechanisms (KEMs), an encryption technique for securing symmetric keys using public key cryptography.
Install Java 21 (OpenJDK 21) on Amazon Linux 2023
There are two methods of installing Java 21 on Amazon Linux 2023:
- Install OpenJDK 21
- Install Java SE Development Kit 21 (JDK 21)
Method 1: Install OpenJDK 21 on Amazon Linux 2023
The GNU General Public License version 2 applies to OpenJDK, which is a free and open-source implementation of the Java Platform, Standard Edition.
To get the most recent archive, go to the JDK 21 releases page.
wget https://download.java.net/java/GA/jdk21/fd2272bbf8e04c3dbaee13770090416c/35/GPL/openjdk-21_linux-x64_bin.tar.gz
Using the tar command, extract the downloaded OpenJDK 21 archive file.
tar xvf openjdk-21_linux-x64_bin.tar.gz
Place the resultant folder in the /opt
directory.
sudo mv jdk-21/ /opt/jdk-21
Now, configure Java environment:
sudo tee /etc/profile.d/jdk.sh <<EOF
export JAVA_HOME=/opt/jdk-21
export PATH=\$PATH:\$JAVA_HOME/bin
EOF
Source your profile file:
source /etc/profile.d/jdk.sh
Check the java command JAVA_HOME
:
$ echo $JAVA_HOME
/opt/jdk-21
Confirm Java version:
$ java -version
openjdk version "21" 2023-09-19
OpenJDK Runtime Environment (build 21+35-2513)
OpenJDK 64-Bit Server VM (build 21+35-2513, mixed mode, sharing)
Method 2: Install Java SE Development Kit 21 (JDK 21) on Amazon Linux 2023
Visit JDK 21 Download page to download .rpm
file for installation.
wget --no-check-certificate -c --header "Cookie: oraclelicense=accept-securebackup-cookie" https://download.oracle.com/java/21/latest/jdk-21_linux-x64_bin.rpm
Now, install the file with .rpm
extension:
$ sudo rpm -Uvh jdk-21_linux-x64_bin.rpm
warning: jdk-21_linux-x64_bin.rpm: Header V3 RSA/SHA256 Signature, key ID ad986da3: NOKEY
Verifying... ################################# [100%]
Preparing... ################################# [100%]
Updating / installing...
1:jdk-21-2000:21.0.7-8 ################################# [100%]
Confirm Java version installed:
$ java -version
java version "21.0.7" 2025-04-15 LTS
Java(TM) SE Runtime Environment (build 21.0.7+8-LTS-245)
Java HotSpot(TM) 64-Bit Server VM (build 21.0.7+8-LTS-245, mixed mode, sharing)
Configuring Java 21 (OpenJDK 21) on Amazon Linux 2023
Configure Java 21 as follows.
Setting Java Default Version
If you have different JDK versions installed, you may change your default Java as follows.
sudo alternatives --list
sudo alternatives --config java
Select Java version to use as default:
$ sudo alternatives --config java
There are 2 programs which provide 'java'.
Selection Command
-----------------------------------------------
*+ 1 /usr/java/jdk-21.0.7-oracle-x64/bin/java
2 /usr/java/java-17-amazon-corretto.x86_64/bin/java
Enter to keep the current selection[+], or type selection number: 1
This will change the system’s Java binary to the one you’ve chosen. Because java
and javac
are handled separately, do the same for them.
sudo alternatives --config javac
Test Java installation on Amazon Linux 2
To test Java 21, we’ll create a Hello World program.
cat > hello_world.java <<EOF
public class helloworld {
public static void main(String[] args) {
System.out.println("Hello World!");
}
}
EOF
Run the code as follows:
$ java hello_world.java
Hello World!
Conclusion
Finally, Java 21 is installed on the Amazon Linux 2023 server. We hope you found this tutorial to be useful. Java coding is a lot of fun.
Cool Guides: