StackTips
 2 minutes

How to set JAVA_HOME Environment Variable MacOS

By Nilanchala @nilan, On Sep 17, 2023 Blog Posts 2.33K Views

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.