How to install Java on Mac OS X and set JAVA_HOME
Download and install latest JDK for Mac ( name ending in macosx-x64.dmg) from http://www.oracle.com/technetwork/java/javase/downloads/jdk8-downloads-2133151.html
Verify installed version of Java after installation is completed from terminal.
$java -version
java version "1.8.0_71"
Java(TM) SE Runtime Environment (build 1.8.0_71-b11)
Java HotSpot(TM) 64-Bit Server VM (build 25.71-b11, mixed mode)
Apple recommends to set the $JAVA_HOME variable to /usr/libexec/java_home. To set JAVA_HOME, just export $JAVA_HOME in file ~/. bash_profile or ~/.profile as follows:
$ cd ~
$ touch .bash_profile
$ open -e .bash_profile
Add following export statement to .bash_profile file
export JAVA_HOME=$(/usr/libexec/java_home)
Close terminal window after setting JAVA_HOME. Open new terminal window and check value of JAVA_HOME.
$ echo $JAVA_HOME
/Library/Java/JavaVirtualMachines/jdk1.8.0_71.jdk/Contents/Home
Now you are ready to install and run java based applications in your Mac.