Install openjdk 16 on mac OS

This article will explain the steps to install openjdk 16 on mac OS and set it on classpath of the system.

Step 1: Download openjdk 16
Go to URL https://jdk.java.net/16.
You will see a page with openjdk 16 bundles for different operating systems listed as below
download openjdk 16 on mac OSClick on the link for macOS and download openjdk 16 bundle.

Step 2: Move the bundle to default location
Java on macOS is installed in /Library/Java/JavaVirtualMachines directory.
Move the downloaded bundle to this location with mv command. So, open terminal, navigate to the location where openjdk 16 bundle was downloaded and execute mv command as shown below

sudo mv openjdk-16.0.1_osx-x64_bin.tar.gz /Library/Java/JavaVirtualMachines/

Step 3: Extract the bundle
Navigate to /Library/Java/JavaVirtualMachines and extract the bundle that was copied in the last step using tar command as shown below.
Also, remove the tar file after it is extracted. Removing file is optional so that unnecessary hard disk space is not occupied.

cd /Library/Java/JavaVirtualMachines
sudo tar -xzf openjdk-16.0.1_osx-x64_bin.tar.gz
rm openjdk-16.0.1_osx-x64_bin.tar.gz

Step 4: Update JAVA_HOME to Java 16
Final step is to set JAVA_HOME environment variable or update it to java 16. For this, open .bash_profile file using vi or nano and add below entry

export JAVA_HOME=/Library/Java/JavaVirtualMachines/jdk-16.0.1.jdk/Contents/Home

To open this file, use command

sudo ~./bash_profile

Add entry for JAVA_HOME, save the file and exit.

If you face any problems in opening or saving the file, use below set of commands,

cd ~
vi .bash_profile

Step 5: Test Java
Openjdk 16 is successfully installed on mac OS. Now, to test whether it is added on the classpath or not, open a new terminal and type

java -version

You should see below output

openjdk version “16.0.1” 2021-04-20
OpenJDK Runtime Environment (build 16.0.1+9-24)
OpenJDK 64-Bit Server VM (build 16.0.1+9-24, mixed mode, sharing)

Openjdk 16 is also added to the classpath.

If you wish to switch java to another version, repeat step 4 and update JAVA_HOME in .bash_profile with desired java version path.

That is all on installing openJDK 16 on mac OS. Hope you have successfully installed it after reading the article.