1. Install Java JDK on Mac

This tutorial step-by-step instructs how to install Java JDK 21 onto Mac.

1.1 Download Java JDK

Download the Java JDK DMG installer file from here

1.2 Install Java JDK

Double click on the downloaded DMG file, then double click on the PKG file and follow the instruction to install Java JDK 21. The Java JDK is installed under /Library/Java/JavaVirtualMachines/

1.3 Set the JAVA_HOME environment variable

First, open a terminal (Launchpad -> Terminal) and run the following command to check which shell your MacOS is using:

echo $SHELL

For bash shell, edit ~/.bash_profile or ~/.bashrc, for zsh shell, edit ~/.zshenv or ~/.zshrc to have the following environment variables:

export JAVA_HOME=/Library/Java/JavaVirtualMachines/jdk-21.jdk/Contents/Home
export PATH=$PATH:$JAVA_HOME/bin

Reload the shell (e.g. bash shell) by running the following command for e.g. the edited .bash_profile file

source ~/.bash_profile

Check the java version using the following command:

java -version

If the output is similar to the screenshot below. Congratulations, you’ve done with installing Java JDK onto your Mac.