- Install the JDKs form Oracle's website (in this example we have JDK 8 and JDK 11).
- Open file
~/.bash_profile
and write the following (this will make sure that the variables are persistent and not just available for the current Terminal session):
export JAVA_8_HOME=$(/usr/libexec/java_home -v1.8)
export JAVA_11_HOME=$(/usr/libexec/java_home -v11)
alias useJava8='export JAVA_HOME=$JAVA_8_HOME'
alias useJava11='export JAVA_HOME=$JAVA_11_HOME'