Skip to content

Instantly share code, notes, and snippets.

@amihaiemil
Last active April 9, 2020 09:24
Show Gist options
  • Save amihaiemil/e2ea35d3575763a05efd3a5e41bf776f to your computer and use it in GitHub Desktop.
Save amihaiemil/e2ea35d3575763a05efd3a5e41bf776f to your computer and use it in GitHub Desktop.
Handle Multiple JDKs MacOs
  1. Install the JDKs form Oracle's website (in this example we have JDK 8 and JDK 11).
  2. 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
  1. 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
  1. 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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment