Apple announced its plan to switch Mac computers from Intel processors to Apple silicon at WWDC 2020. Apple silicon is a system on a chip (SoC) and system in a package (SiP) processor designed by Apple Inc., mainly using the ARM architecture

Apple’s custom chips are Arm-based and are similar to the A-series chips used in iPhones and iPads, making them markedly different from the Intel chips that were used in earlier Macs. The MacBook Pro, MacBook Air, Mac Mini, Mac Studio, iMac, ‌Mac Pro‌, and iPad Pro all use variants of the M2 chip.

In this tutorial, we’re going to install Java/JDK and Set up the Java Home path. Let’s go to the Oracle site and download DMG Installer.

Download Java ARM64 DMG Installer

If you’re using Intel Core then download x64 DMG Installer. If you’re using Apple Silicon M1/M2 Chip then you have to Download ARM64 DMG Installer. I have an Apple M2 Chip so I am going to download Arm 64 DMG Installer.

Then open ARM64 DMG Installer and double-click. This is going to open the installer.

Install JDK in Mac M2 Chip

Continue-> Install->Input Password->Install Software and this is going to install Java. Now, what you have to do is set Java Home so that other Applications that use Java know which Java binary to run.

Set the $JAVA_HOME Environment Variable Z shell

The reason why the Java installer doesn’t care about this is because you might be able to have multiple Java version and you want to be able to switch between Java versions depending on what you’re making and what your software require.

First, open up a terminal, If you have a new MacOS Apple recently changed their default terminal to Z shell. If you have an older MacOS you might use Bash shell. Depending on what you have” how you set up java home might be different”.You have to make sure which shell you’re using and this is how you can do it terminal.

echo $SHELL

If you get “/bin/zsh” this one, then you’re using Z shell but instead if you get “/bin/bash" then you’re using Bash script. Right now I am going to show you how to do it in Z shell.

The next step is to run ls -al command in the terminal.

Set Java Home Environment Variables

This is going to list out all the files and folders that you have in that specific directory. If you have a Z shell you might have .zshenv file. If you don’t have it run the following command in the terminal.

touch ~/.zshenv 

The above command creates .zshenv file.

Find Java Path in Mac Terminal

Next, we’re going to figure out where Java binary or JDK binary is located. In order to find out, run the flowing command in the terminal.

/usr/libexec/java_home
Find Java Path in Mac Terminal

 Run the following command in terminal to open .zshenv file.

open ~/.zshenv       

Copy and paste following path in .zshenv file.

#Replace this JDK Path
export JAVA_HOME=/Library/Java/JavaVirtualMachines/jdk-21.jdk/Contents/Home 

Save .zshenv and close the file and then we’re going to do is tell shell that we actually change something so apply the change into the current shell.

source ~/.zshenv

Now let’s check if java home is set.Run the following command in terminal which is return $JAVA_HOME path from .zshenv file.

echo $JAVA_HOME
Check Java Version

Set the $JAVA_HOME Environment Variable Bash shell

Now I’ll show you how to do this exact same thing if you’re using bash script or bash shell.If you’re using bash script you need the following file.

#in terminal
touch ~/.bash_profile

open ~/.bash_profile 

I’am going to open up that file and add java home path.

#Replace this JDK Path
export JAVA_HOME=/Library/Java/JavaVirtualMachines/jdk-21.jdk/Contents/Home 

If you install different versions of java you can change this version.Here we installed java jdk21 so we’re gonna have jdk21 here and then save and then exit. From terminal run the following command.

source ~/.bash_profile 

This is going to tell shell to pickup up updated bash profile and then use those environment variables.