For macOS Sierra 420
This guide was cobbled together from various sources (replies above as well as other posts), and works perfect.
0. If you haven't already, install homebrew.
See https://brew.sh/
1. Install jenv
brew install jenv
2. Add jenv to the bash profile
if which jenv > /dev/null; then eval "$(jenv init -)"; fi
3. Add jenv to your path
export PATH="$HOME/.jenv/shims:$PATH"
4. Tap "homebrew/cask-versions"
FYI: "Tap" extends brew's list of available repos it can install, above and beyond brew's default list of available repos.
brew tap homebrew/cask-versions
5. Install the latest version of java
brew install java --cask
6. Install java 6 (or 7 or 8 whatever you need)
brew install java6 --cask #brew install java7 --cask #brew install java8 --cask
? Maybe close and restart Terminal so it sees any new ENV vars that got setup.
7. Review Installations
Most Java version get installed here: /Library/Java/JavaVirtualMachines lets take a look.
ls -la /Library/Java/JavaVirtualMachines
NOTE: Sometimes java ends up here: /usr/local/opt/openjdk/bin/java, so you can peek into the "opt" dir and look for "openjdk" in the list with:
ls -la /usr/local/opt/
8. Add each path to jenv one-at-a-time.
We need to add "/Contents/Home" to the version folder. WARNING: Use the actual paths on your machine... these are just EXAMPLE's
jenv add /Library/Java/JavaVirtualMachines/1.6.0___EXAMPLE___/Contents/Home jenv add /Library/Java/JavaVirtualMachines/jdk-9.0.1.jdk___EXAMPLE___/Contents/Home
TIP: If you think you already have java, but not sure where it is, try using "which" -- if you have java installed, "which" should print out the path to java:
which java
9. Check if jenv registered OK
jenv versions
10. Set java version to use (globably)
Where XX matches one of the items in the versions list above.
jenv global XX
Check java version
java -version
NOTE: If not seeing version, enable plugin export (see comments below from cakraww):
jenv enable-plugin export
Check jenv versions
Should also indicate the current version being used with an asterisk.
jenv versions
DONE
Quick future reference
To change java versions
... See the list of available java versions
jenv versions
... then, where XX matches an item in the list above
jenv global XX
... also check which java the system recognizes (see TIP in #8 above)
which java