This is an easy solution to manage differend JDKs on your Mac.

1. Install Homebrew

mkdir homebrew && curl -L https://github.com/Homebrew/brew/tarball/master | tar xz --strip 1 -C homebrew

You can find more informations about homebrew at https://docs.brew.sh/Installation.

2. Find and install Java versions

Add the homebrew/cask-versions tap to Homebrew with brew tap caskroom/versions.

With brew search java you can find all available java versions.

To install a specific JDK type brew cask install <java_version>. For example:

  • brew cask install java
  • brew cask install java8

3. Install Jenv and add your Java versions

To install jenv: brew install jenv.

For more information on jenv visit http://jenv.be/.

Too add java versions to jenv type jenv add <java_home_path>. For our example we add our java and java8 versions to jenv:

  • jenv add /Library/Java/JavaVirtualMachines/openjdk-11.0.1.jdk/Contents/Home/
  • jenv add /Library/Java/JavaVirtualMachines/jdk1.8.0_192.jdk/Contents/Home/

4. Update you bash profile

1
2
3
$ echo ‘export PATH=”$HOME/.jenv/bin:$PATH”’ >> ~/.bash_profile
$ echo ‘eval “$(jenv init -)”’ >> ~/.bash_profile
$ source ~/.bash_profile

4. List and configure your Java versions with jenv

To list all available versions in your jenv:

1
2
3
4
5
6
7
8
$ jenv versions
* system
1.8
1.8.0.192
11.0
11.0.1
openjdk64-11.0.1
oracle64-1.8.0.192

You have the choice to configure the version globaly, localy or for the shell like this:

  • gloabl: jenv global oracle64-1.8.0.192
  • local: jenv local oracle64-1.8.0.192
  • shell: jenv shell oracle64-1.8.0.192

Check the current version with jenv version.

Voilà.