Guide to Setup OpenJDK 22 on macOS
- Details
- Written by Nam Ha Minh
- Last Updated on 05 July 2024   |   Print Email
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](/images/articles/javase/install-jdk-22/OpenJDK_22_download_page_macOS.png)
- 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).
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.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/OpenJDK22This 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](/images/articles/javase/install-jdk-22/verify_openjdk_22_installation_macos.png)
Related Articles:
- How to set JAVA_HOME in macOS and Linux permanently
- How to set JAVA_HOME environment variable on Windows 10
- What are JVM, JRE and JDK
- How to write, compile and run a hello world Java program for beginners
- Java Core Language Tutorials
About the Author:
![](/images/NamAuthor.png)
Comments