How to install Java 21 and setup JAVA_HOME Path in macOS 🍏 (Apple Silicon M1 & M2)

Aniket Vishal
3 min readSep 23, 2023

--

If you’re a proud owner of an Apple Silicon M1 or M2 Mac, you may be wondering how to install Java and set up the JAVA_HOME path. Java is a versatile programming language that’s used in various applications, and having it properly installed on your macOS is essential for developers and tech enthusiasts. In this guide, we’ll walk you through the steps to get Java up and running on your Apple Silicon Mac.

Step 1: Download the Appropriate Java Version ☕

Apple Silicon Macs use a different architecture compared to their Intel-based predecessors. Therefore, you need to download a Java version that’s compatible with Apple Silicon.

  1. Open your web browser and open the Java Downloads section on Oracle website.
  2. Select the “Latest Java Release” for macOS.
  3. Click the ARM64 DMG Installer download link to save the file to your computer.
Screenshot for downloading java from Oracle website
You need to download the selected Java installer for Apple Silicon Mac.

Step 2: Install Java 🛠️

Now that you’ve downloaded the appropriate Java version, it’s time to install it on your Mac:

  1. Locate the downloaded file (usually in your “Downloads” folder) and double-click it to open the installation package.
  2. Follow the on-screen instructions to complete the installation process. This will involve accepting the license agreement.

Step 3: Verify Your Java Installation ✔️

To ensure that Java is correctly installed open your Terminal and type:

java --version

You should see Java version displayed on the screen.

Screenshot showcasing Verify Your Java Installation
Verify your Java Installation

Step 4: Set Up JAVA_HOME 🏠

To make Java easily accessible for development purposes, you’ll have to set up the JAVA_HOME environment variable. Please follow the following steps to setup JAVA_HOME path in your macOS.

  • Open the Terminal application (you can find it in the “Utilities” folder within the “Applications” folder).
  • Create the zShell Environment file if you don’t have it. The Environment file should be stored at the root directory of your mac and it’s name should be “.zshenv”. Use the below code to create your zShell Environment file.
touch ~/.zshenv
  • Use the following code to open the zShell Environment file.
open ~/.zshenv
  • Add the below path in your zShell Environment file and save it.
export JAVA_HOME=$(/usr/libexec/java_home)
Screenshot showcasing JAVA_HOME path in .zshenv file
JAVA_HOME path added in the zShell Environment file (.zshenv)
  • Enter the following code in the terminal after you saved and closed the environment file to apply the changes and refresh your profile.
source ~/.zshenv

Your JAVA_HOME path is set successfully in the zShell Environment file. Cheers🍻

Step 5: Verify Your JAVA_HOME Setup ✔️

To ensure that Java is correctly installed and the JAVA_HOME path is set up, open your Terminal and type:

echo $JAVA_HOME
Screenshot showcasing JAVA_HOME path in macOS terminal
Your JAVA_HOME path displayed on the screen.

Conclusion 🎉

Congratulations! You’ve successfully installed Java and set up the JAVA_HOME path on your Apple Silicon M1 or M2 Mac. You’re now ready to start coding and developing Java applications with ease.

Remember to keep your Java installation up-to-date and explore the exciting world of Java development on your powerful Apple Silicon Mac! 💻🍏✨

If you have any questions or run into any issues during the installation process, feel free to reach out to the Java community or consult the official documentation for further assistance. Happy coding! 🚀👨‍💻👩‍💻

--

--