StackTips

How to set JAVA_HOME Environment Variable MacOS

nilan avtar

Written by:

Nilanchala,  2 min read,  updated on September 17, 2023

In this tutorial, we show you how to set $JAVA_HOME environment variable on your Mac OS X.

Mac OSX 10.5.x or later

Open the Terminal program present in your  (this is in your Applications/Utilities folder by default). Follow the below steps.

Step-1 Open and edit .bash_profile file

open -e .bash_profile

Step-2 Save the below line

export JAVA_HOME=$(/usr/libexec/java_home

Step-3 Refresh the file using below command

$ source .bash_profile

Step-4 Check if the JAVA_HOME is set properly

$ echo$JAVA_HOME
/Library/Java/JavaVirtualMachines/1.7.0.jdk/Contents/Home

For Older Mac OS X

For older Mac OS X, you have to use the full path of installation directory as it /usr/libexec/java_home doesn’t exists.

$ open -e .bash_profile
$ exportJAVA_HOME=/System/Library/Java/JavaVirtualMachines/1.6.0.jdk/Contents/Home
$ source .bash_profile

$ echo$JAVA_HOME
/System/Library/Java/JavaVirtualMachines/1.6.0.jdk/Contents/Home