Setting up Java Development Kit (JDK) is the first and foremost step to start developing Java applications. A JDK provides Java Virtual Machine (JVM) that is capable of executing bytecode compiled from .java source files, Java compiler (javac) and other tools (jar, javap, javadoc…) that aid the development process.

In this guide, you’ll see how to download and install JDK version 22 on macOS step by step, with OpenJDK - a free and open-source distribution of Java Development Kit. JDK 22 is a regular update which was released on March 2024.

NOTES: you may need to check Java version on your Mac beforehand. If you have an outdated one, uninstalling it following this instruction.

 

1. Download Binary Distribution of OpenJDK 22 for macOS

OpenJDK is distributed as tar.gz archive file for macOS (no executable installer). It’s strongly recommend to download from official source, so head over to Open JDK 22 download page, you will see the following page:

OpenJDK 22 download page macOS

You can see there are two different download links, which corresponding to different Mac hardware:

  • If your Mac runs on Intel CPU, click the tar.gz link next to macOS/x64
  • Else if your Mac runs on Apple CPU, e.g. Apple M1, click the tar.gz link next to macOS/AArch64 (marked number 1 in the above screenshot).
Save the tar.gz file in a desired destination, usually in Downloads directory.


2. Verify Downloaded Archive File

It’s strongly recommended to check the integrity of the downloaded archive file before extracting it. On the download page, click the sha256 link next to the download link, you’ll see the SHA256 checksum of the original file.



Now open a new Terminal window, change the current directory to the location of the archive file, and type the following command:

shasum -a 256 openjdk-22*.tar.gz

This command prints the SHA256 checksum of the specified file. If it matches the one provided on the download page, you can safely continue. Else the file was corrupted or tampered with.


3. Install OpenJDK 22 on macOS

Now, to install OpenJDK 22 on your Mac you need to extract the tar.gz file and configure some environment variables.

Say, you want to install it in OpenJDK22 directory which is under your user home directory, type the following command to extract the archive file:

                tar -xf openjdk-22*.tar.gz -C $HOME/OpenJDK22

This command will extract the files of OpenJDK 22 into something like jdk-22.0.1.jdk/Contents/Home directory under the specified directory.

Next, you should create/update the shell’s resource file to define JAVA_HOME variable and update PATH variable so you will be able to execute JDK’s executables anywhere in command line. Also, other programs that depend on JDK will find the default JDK easily.

So in Terminal, make sure the current directory is your user home. Type the following command to appends new content into the .zshrc file (Z-Shell):

cat >> .zshrc

Then type the following content:

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

export PATH=$JAVA_HOME/bin:$PATH

The first line exports an environment variable named JAVA_HOME with value points to the installation directory of OpenJDK 22. And the second line updates the PATH environment variable to include a path of JAVA_HOME.

Press Ctrl + D to save the file. If Bash is the default shell, then you need to alter the .bash_profile file instead.


4. Verify OpenJDK 22 Installation on macOS

Quit the Terminal and open a new one so the shell’s resource file will be reloaded. Then type java -version to check version of Java virtual machine, and type javac -version to check version of Java compiler. You will see the following output:

verify openjdk 22 installation macos

This means OpenJDK 22 has been successfully installed on your macOS. Congratulations! You can now begin creating, developing, running applications using Java platform.

You can also watch the following video to see the setup of OpenJDK 22 on macOS in action:

 

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