How to set Java_Home variable in MAC

How to set JAVA_HOME on MAC

I shifted from Windows OS to MAC. And I face little time while installing and setting up JAVA_Home variable on the MAC. Searching multiple websites I find out that after MAC OS 10.5, Apple recommend a different way to set JAVA_HOME variable. I will mention both.

In MAC OS 10.6 or above you have to set the JAVA_HOME variable in the .bash_profile. If you will not set the path it will not work. Before setting up the variable make sure that JDK is installed on your machine. To check the Java version on the Mac, open the terminal window (press cmd + space and type terminal),  and type java -version in the terminal window, if JDK is properly installed you will get the output something like shown in the image below,

If JDK is not installed, you can download from the Oracle website. Once the installation is completed open the terminal window and type the following command.

echo export "JAVA_HOME=\$(/usr/libexec/java_home)" >> ~/.bash_profile

For the Mac OS 10.6 or above as I mentioned before it is important to add the java home in your bash profile.  Once you execute the command on the terminal window, to verify if the path is properly set or not run the following command on the terminal window echo $JAVA_HOME . And you will get the output like shown in the image below.

If you are using MAC OS version 10.5 or below you can type the following command in the terminal window to set the JAVA_HOME variable export JAVA_HOME=`/usr/libexec/java_home .

If you experienced MAC user and know how to edit .bash_profile, then you can use the following way to add JAVA_HOME variable in the .bash_profile. Open the terminal window and type the following lines to edit .bash_profile

$ vim .bash_profile
export JAVA_HOME=$(/usr/libexec/java_home)

Save and exit from the vim editor and open a new terminal window and type echo $JAVA_HOME  to check if the variable is successfully installed or not.

Leave a Reply