HomeJavaHow to set $JAVA_HOME environment variable on Mac OS X

How to set $JAVA_HOME environment variable on Mac OS X

In this journal entry we will see how we can setup the $JAVA_HOME environment variable on Mac OS X 10.5 or later versions.

From OS X 10.5, Apple introduced a command line tool (/usr/libexec/java_home) which dynamically finds the top Java version specified in Java Preferences for the current user.

Open ~/.bash_profile or ~/.zshrc as sudo in any text editor and add:

export JAVA_HOME=$(/usr/libexec/java_home)

Save and close the file. Once this step is done now all you have to do is run the below mentioned command and you will be all set in using the JAVA_HOME.

source ~/.bash_profile


source ~/.zshrc

After running the above commands, run the following command to verify that the JAVA_HOME environment variable is setup properly on your machine.


Above mentioned command should display the results like the one shown below. This is the path where JDK is installed.


Global Setting

The above mentioned commands will set the JAVA_HOME environment variable for a single user. What if you want that this variable should be set globally for all the users who will be using the machine?

The answer to this question is the file /etc/profile. The steps will remain same.


Most Popular