Skip to content

Instantly share code, notes, and snippets.

@shreeshkatyayan
Last active June 18, 2018 00:48
Show Gist options
  • Save shreeshkatyayan/6f60c851e06d661ff7a8c6376d9789df to your computer and use it in GitHub Desktop.
Save shreeshkatyayan/6f60c851e06d661ff7a8c6376d9789df to your computer and use it in GitHub Desktop.

Java Multi-versions

This tutorial is specific to OSX users currently. I haven't tested it anywhere else. If you can help in writing for other environments, please do contribute.

Install Homebrew cask

brew update
brew tap caskroom/cask
brew install brew-cask-completion
brew tap caskroom/versions

Check info for Java versions

brew cask info [java version]

Install Jenv - Multiple Java versions manager

brew install jenv
echo 'export PATH="$HOME/.jenv/bin:$PATH"' >> ~/.profile
echo 'eval "$(jenv init -)"' >> ~/.profile

Install multiple Java versions

brew-cask provides a friendly homebrew-style CLI workflow for the administration of macOS applications distributed as binaries

brew cask install [java version]

Now as the jenv doesn't know about the multiple java versions that are installed, we will add them manually!

jenv add /Library/Java/JavaVirtualMachines/[jdk versions here]/Contents/Home

You can automate this by the script after this literature (jenv_add.sh)

Switch java versions

You can now switch versions easily by

jenv global/local/shell [java version]
LIST=`ls /Library/Java/JavaVirtualMachines`
echo "=========================================\nAvailable Java versions:\n$LIST\n========================================="
for I in ${LIST//$'\n'/ }
do
jenv add /Library/Java/JavaVirtualMachines/$I/Contents/Home
echo "Added $I to jenv"
done
echo "=========================================\nVersions now available in jenv:\n$(jenv versions)\n========================================="
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment