build a command line java switch

Add this to your ~/.zshrcafter both versions are installed:

# export JAVA_HOME=$(/usr/libexec/java_home)

export JAVA_8_HOME=$(/usr/libexec/java_home -v1.8)
export JAVA_11_HOME=$(/usr/libexec/java_home -v11)

alias java8='export JAVA_HOME=$JAVA_8_HOME'
alias java11='export JAVA_HOME=$JAVA_11_HOME'
java8

To switch no simple execute:

# or switch to java11, default in this example is 8
java11

# ...your stuff will be executed with java11

Credits to: original post - with deeper explanation