Let me check the current Java version
$ java -version
java version "13.0.2" 2020-01-14
Java(TM) SE Runtime Environment (build 13.0.2+8)
In macOS Java is installed as part of /Library/Java/JavaVirtualMachines folder.
Let me go there.
$ cd /Library/Java/JavaVirtualMachines/
I want to set Java 11 as the default Java version. Let me check what all I've installed in my system.
$ pwd
/Library/Java/JavaVirtualMachines
$ ls
jdk-11.0.7.jdk jdk-13.0.2.jdk jdk1.8.0_201.jdk
Let me set the default Java version to jdk-11.0.7.jdk
$ export JAVA_HOME=/Library/Java/JavaVirtualMachines/jdk-11.0.7.jdk/Contents/Home/
$ export PATH=$JAVA_HOME/bin:$PATH
The above changes are applicable only for the current shell. To change permanently for all the shells you need set those two lines in the .bash_profile
First, go to the user home directory to run the below command
$ cd ~
$ pwd
/Users/kondal
$vim .bash_profile
Append the above two lines in the .bash_profile at the end, and save and exit.
All set.
Check again!
$ java --version
java 11.0.7 2020-04-14 LTS
Java(TM) SE Runtime Environment 18.9 (build 11.0.7+8-LTS)
Java HotSpot(TM) 64-Bit Server VM 18.9 (build 11.0.7+8-LTS, mixed mode)
Good to go!!