StackTips

How to set JAVA_HOME Environment Variable MacOS

nilan avtar
Nilanchala Panigrahy 馃尡

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鈥檛 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

Sharing is caring!

Did you like what Nilanchala Panigrahy 馃尡 wrote? Thank them for their work by sharing it on social media.

nilan avtar

Nilanchala Panigrahy 馃尡

I'm a blogger, educator and a full stack developer. Mainly focused on Java, Spring and Micro-service architecture. I love to learn, code, make and break things.

Related articles