StackTips

How to set JAVA_HOME Environment Variable MacOS

nilan avtar

Written by

Nilanchala,  2 min read,  2.79K views, updated on Sept. 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
nilan avtar

Nilanchala

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 posts

Let’s be friends!

🙌 Stay connected with us on social media for the latest updates, exclusive content, and more. Follow us now and be part of the conversation!