Finding the JAVA_HOME in your Mac

Generally if you are not sure where your JAVA_HOME is the first thing you would do is type in the following command

echo $JAVA_HOME

But more times than usual if you have not intentionally set this variable in your MAC you would see an empty response. So now how would you proceed to find the real JAVA_HOME.  Some times a quick solution is looking in /System/Library/Frameworks/JavaVM.framework/Versions. Still there can be a lot of Java HOME's in there (like what happened to me).

java

So now how do we look for the JAVA_HOME or to which folder do we point to. 

1. First run

java -version

2. Now you can straight away point to the HOME folder according to the version if there is a  folder with the particular version. But the problem is sometimes there is no such folder.

version folder

3. Now issue the following commands to find the actual folder.

finding the folder

Now run the java_home command in the same Commands folder.

java_home command

The java_home command usually returns the value for $JAVA_HOME 

https://developer.apple.com/legacy/library/documentation/Darwin/Reference/ManPages/man1/java_home.1.html

so now this :/Library/Java/JavaVirtualMachines/jdk1.8.0_91.jdk/Contents/Home should be  your JAVA_HOME

add the following lines to your .bash_profile

vi ~/.bash_profile

#add the following line

export JAVA_HOME=/Library/Java/JavaVirtualMachines/jdk1.8.0_91.jdk/Contents/Home

#save the file and now issue the following command

source ~/.bash_profile

Now when you print $JAVA_HOME you should see that its correctly set

java home