Setting up JAVA_HOME on MacOS

Anupindi RaviShankar
2 min readFeb 16, 2022

In this article ,we will see how to setup JAVA_HOME on MacOS. After having shifted to MacOS from Windows, I found it really difficult to set JAVA_HOME. On Windows, it was quite straightforward as we have to simple open Environment variables window and set the JAVA_HOME and PATH accordingly. Though it is quite simple in MacOS too, but being a newbie , it took me some time to explore and set the things.

Checking the Java version installed

  1. First check if Java is installed or not by simply typing the below commands on terminal window
which java 
This command will tell the location where java is installed
java -version
You should see some output like this which confirms that Java is installed
java version "1.8.0_202"Java(TM) SE Runtime Environment (build 1.8.0_202-b08)Java HotSpot(TM) 64-Bit Server VM (build 25.202-b08, mixed mode)

2. Next check which all Java versions are installed. For this, open the terminal window and type the below command

/use/libexec/java_home

and you will see some output like below.

/Library/Internet Plug-Ins/JavaAppletPlugin.plugin/Contents/Home

In order to find all Java versions which are installed we need to enter the below command which will output all versions of Java installed on your MacOS

/usr/libexec/java_home -V### OUTPUT #####Matching Java Virtual Machines (2):1.8.202.08 (x86_64) “Oracle Corporation” — “Java” /Library/Internet Plug-Ins/JavaAppletPlugin.plugin/Contents/Home1.8.0_202 (x86_64) “Oracle Corporation” — “Java SE 8” /Library/Java/JavaVirtualMachines/jdk1.8.0_202.jdk/Contents/Home

The one above in bold is the JDK which I have installed, but that is not the default JDK as seen when we ran the echo $JAVA_HOME command and I want to make the one in bold as the default JDK. To do that we have to set JAVA_HOME to point to the JDK which I have installed and not the default one which was shipped with macOS. In the next section we will see how we can achieve the same.

Make JAVA_HOME to point to the correct JDK

Below are the steps which we need to follow

  1. First we will print environment variables using the below command on terminal window.It is one way to know what all environment variables are set and you will be able to see JAVA_HOME also.
printenv

2. To permanently set the JAVA_HOME point to my JDK, I have to create Z Shell files (~/.zshrc or ~/.zshenv ). if these files are already present in your system then you don’t have to create them.To create new one use the below command

touch ~/.zshrc or touch ~/.zshenv

3. Now open the above file using the below command

open -a TextEdit.app ~/.zshenv

and add the line as mentioned below

export JAVA_HOME=/Library/Java/JavaVirtualMachines/jdk1.8.0_202.jdk/Contents/Home

save the file and close it. Now open another Terminal window and type the below command

echo $JAVA_HOME### OutPut ###/Library/Java/JavaVirtualMachines/jdk1.8.0_202.jdk/Contents/Home

As seen above JAVA_HOME is correctly set pointing to my JDK. Thats it.

Hope you find this article useful. Happy Learning !!!

--

--