So you have Java installed on Mac OS X and you have a program that needs the JAVA_HOME variable set, but you don’t know where Java’s home directory is. This is an easy way to find out. Open Terminal and run the following command:
/usr/libexec/java_home
It should spit out something like this which is the home location:
/Library/Java/JavaVirtualMachines/jdk1.8.0_31.jdk/Contents/Home
Now if you have a program that launches via script or command line and needs JAVA_HOME set, you can add this to the script to set it:
export JAVA_HOME="$(/usr/libexec/java_home)"