Skip to content

Instantly share code, notes, and snippets.

@ng-the-engineer
Created April 5, 2020 12:47
Show Gist options
  • Save ng-the-engineer/eadf81bca1bd98b8814142fdec8da706 to your computer and use it in GitHub Desktop.
Save ng-the-engineer/eadf81bca1bd98b8814142fdec8da706 to your computer and use it in GitHub Desktop.

Switch JDK versions on Mac OS

Install jenv

$ brew install jenv

Add jenv to PATH variable in shell

Bash

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

Zsh

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

If it does not take effect, restart your shell

Verify jenv

$ jenv versions

It shows the registered JDK version(s)

* system (set by /Users/YOUR_USER_NAME/.jenv/version)

Register a new JDK version

After you have installed a new JDK, register it in jenv, for example

$ jenv add /Library/Java/JavaVirtualMachines/jdk1.8.0_202.jdk/Contents/Home

By default, you may find your installed JDK in this path

/Library/Java/JavaVirtualMachines/

Verify again

$ jenv versions
* system (set by /Users/YOUR_USER_NAME/.jenv/version)
  1.8
  1.8.0.202
  oracle64-1.8.0.202

Switching JDK, for example, to 1.8

jenv global 1.8
More information on http://www.jenv.be/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment