How to Setup OpenJDK 21 on macOS
- Details
- Written by Nam Ha Minh
- Last Updated on 31 December 2023   |   Print Email
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: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
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: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:
- 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
Comments