- 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
- 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'
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 andjEnv
will take care of switching the JDK automagically.