How to set the Java version on Mac OS X (macOS) systems

I don’t remember where I first found this line of code, but if you put it in your Mac OS X ~/.bash_profile file, it’s an easy way to set your Mac Java version:

export JAVA_HOME=`/usr/libexec/java_home -v 1.8`

I can confirm this works with the Bash shell on Mac OS X 10.10. When I run the java -version command after opening a new Mac Terminal window, the output is 1.8.0_25.

A slightly more difficult way to set your Mac Java version is to look under the /Library/Java/JavaVirtualMachines directory to see which versions are installed, and then manually set the version.