Skip to content

Instantly share code, notes, and snippets.

@eliflores
Last active February 4, 2021 21:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save eliflores/c2b8c4610d9780becef1824ee021c622 to your computer and use it in GitHub Desktop.
Save eliflores/c2b8c4610d9780becef1824ee021c622 to your computer and use it in GitHub Desktop.
Java versions

Install and use different Java Versions

Source: https://stackoverflow.com/questions/52524112/how-do-i-install-java-on-mac-osx-allowing-version-switching

Installation

  • Update the formulae and Homebrew itself
brew update
  • Add the casks tap
brew tap homebrew/cask-versions
  • Look for Java Versions
brew search java
  • Check the details on the version that will be installed
brew cask info java
  • Install the latest version available:
brew cask install java
  • To find locations of previously installed Java JDK's installed at the default system locations:
/usr/libexec/java_home -V

To manually switch between Java versions:

  • Add to your .bash_profile different env variables for the different Java Versions:
export JAVA_8_HOME=$(/usr/libexec/java_home -v1.8)
export JAVA_11_HOME=$(/usr/libexec/java_home -v11)
export JAVA_14_HOME=$(/usr/libexec/java_home -v14)
  • Create different aliases to switch between versions
alias java8='export JAVA_HOME=$JAVA_8_HOME'
alias java11='export JAVA_HOME=$JAVA_11_HOME'
alias java14='export JAVA_HOME=$JAVA_14_HOME'
  • Default to whatever version you want in your .bash_profile
java8
  • Install Java

  • Note: Tested with Java 15

brew install adoptopenjdk
  • Search for Java casks:
brew search --casks --desc 'adoptopenjdk'

Manage your Java Environment with jEnv

jEnv is a command line tool to help you forget how to set the JAVA_HOME environment variable.

  • By using jEnv, you could also add a .java-version to each of your Java projects and jEnv will take care of switching the JDK automagically.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment