To develop Java applications, a Java Development Kit (JDK) must be installed in the host operating system. In this quick guide, I’d like to share with you the steps to setup Java 21 on macOS with OpenJDK - an open-source distribution of JDK.

You know, JDK 21 or Java 21 is the current Long Term Support (LTS) release of Java SE platform (as of December 2023). An LTS release means that it is the major version which will be supported for a long time.

 

1. Download Binary Distribution of OpenJDK 21

OpenJDK is distributed as tar.gz archive file for macOS. To download binary distribution of OpenJDK 21, head over to its official download page, then you’ll see the following page:

OpenJDK 21 download page

Click the link tar.gz (1) to download the archive file. Depending on your Mac’s CPU type, you can choose either:

  • macOS/AArch64: if your Mac running with Apple chip (Apple Silicon)
  • macOS/x64: if your Mac has Intel CPU inside
For example, my Mac computer has Apple M1 chip so I click the link tar.gz next to macOS/AArch64, then I’ll get the file openjdk-21.0.1_macos-aarch64_bin.tar.gz downloaded on my computer.

It’s important to verify the integrity of the downloaded file before extracting it. You can see the link sha256 (2) next to the link tar.gz, which contains the SHA256 checksum of the original file, for verification purpose.

So on macOS, open a new Terminal window. Change the current directory to the location where the archive file is downloaded. And type the following command:



      shasum -a 256 openjdk-21.0.1_macos-aarch64_bin.tar.gz

Compare the SHA256 hash value printed by this command against the one published on the official download page (link 2). If both are identical, you can safely install. Otherwise the file was tampered with somehow along the way.


2. Install OpenJDK 21 on macOS

To start installation of OpenJDK 21 on macOS, extract the downloaded tar.gz file into a desired directory. In terminal, you can use the following command:

tar -xf openjdk-21.0.1_macos-aarch64_bin.tar.gz -C $HOME/OpenJDK

This will extract the tar.gz file into OpenJDK directory which is under your user home directory. Then peform some ls commands, you will see the JDK home directory is at jdk-21.0.1.jdk/Contents/Home.

The next step is defining a new system environment called JAVA_HOME and updating the existing one called PATH. If you’re using Z-Shell, use the following commands that append 2 lines to the Z-Shell resource file (.zshrc), which is under your user home:

cat >> .zshrc

export JAVA_HOME=$HOME/OpenJDK/jdk-21.0.1.jdk/Contents/Home

export PATH=$JAVA_HOME/bin:$PATH

To save the file, enter a new line and press Ctrl + D. If current shell is Bash, you need to update the .bash_profile file instead.

Finally, let’s verify the setup of OpenJDK by checking the version of Java virtual machine and Java compiler. Quit the current terminal completely, then open a new one. Type java -version to check version of Java virtual machine, and type javac -version to check version of Java compiler, as shown below:

openjdk 21 check version macos

If you see the output similar to this, congratulations! You have set up OpenJDK 21 on your Mac successfully. You can now begin developing Java applications on macOS.

To see the setup of OpenJDK 21 on macOS in action, watch the following video:

 

Related Articles:


About the Author:

is certified Java programmer (SCJP and SCWCD). He began programming with Java back in the days of Java 1.4 and has been passionate about it ever since. You can connect with him on Facebook and watch his Java videos on YouTube.



Add comment

   


Comments 

#1programervn2024-01-30 21:02
for development, I think using SDK MAN should be easier
Quote