How To Have Multiple Versions Of Java On Your macOs

Challenge

You want to have multiple versions of Java available on your Mac…

Prerequisites

  • brew installed
  • ~/bin folder on your path

Solution

  • brew install jenv to install JEnv
  • Add versions tap room to brew: brew tap caskroom/versions
  • Install your java versions, e.g.:
    • brew cask install java
    • brew cask install java7
    • brew cask install java8
  • Add a file called mvn to your ~/bin folder with the following content
1
2
3
#!/bin/sh
export JAVA_HOME=$(jenv javahome)
/usr/local/bin/mvn "$@"
  • You can now switch between java versions with the jenv command
  • Do not forget to create a new terminal session for all this to take effect (just stop and start your terminal)