Easily switch between Java versions on macOS

Check which java versions are installed on your system (JRE & JDK)

$ /usr/libexec/java_home -V

Run the line below in your terminal. Just replace version's number with the one you want (in this case 1.8)

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

If you have multiple subversions of Java, it will choose the highest available if you don't specify one.

then verify your version running with

$ java -version