You can add these alias in your ~/.bash_profile
file and easily switch between different JAVA versions.
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’
To check the available java versions in your system use
jenv versions
jenv version
You can even set particular version for single project by using following command in private terminal.
jenv local 11.0
# Seconds option :-
Download adopt jdk from your choise from file -> project structure -> project sdk
and then add following function in your .zshrc to switch between different version as per requirement.
[Attaching screenshot for reference:] :-

Switch between different JDK versions
Add the below function in your ~/.bashrc or ~/.zshrc
jdk() {
version=$1
export JAVA_HOME=$(/usr/libexec/java_home -v"$version");
java -version
}
- Source the profile and you can change the version like below:
jdk 1.8
jdk 9
jdk 11
jdk 13