- 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'
#default java8
export JAVA_HOME=$JAVA_8_HOME
- Now the default is JDK 8 and this will be visible with both
java
and mvn
commands:
$ java -version
...print JDK 8 info
$ mvn --version
...print Apache Maven info
...print JAVA_HOME pointing to JDK 8
- When you want to switch between versions, just use the delcared aliases. E.g. switching to JDK 11:
$ useJava11
$ java -version
...print JDK 11 info
$ mvn --version
...print Apache Magen info
...print JAVA_HOME pointing to JDK 11