Skip to content

Instantly share code, notes, and snippets.

@jonjack
Last active September 10, 2024 18:52
Show Gist options
  • Save jonjack/898efa5ccc79c140533ed53e0b1edbad to your computer and use it in GitHub Desktop.
Save jonjack/898efa5ccc79c140533ed53e0b1edbad to your computer and use it in GitHub Desktop.
Install Java on Mac OSX via Home-brew

Notes on installing multiple versions Java on Mac OSX using Homebrew and Jenv.

Install Homebrew

Homebrew is a superb tool for Mac for managing the installation of software. It's core command is brew and it calls packages of software formula. If you are intrigued then you can check out the list of Formula in the brew Github repository.

/usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"

By deafult, Brew manages the installation of command line software. If you are installing software distributed as a binary then Brew can manage that as well with the Cask extension. Binaries are typically managed in different libraries to the brew formula. So we will use the tap command to add the following repositories which give us access to bionary libraries where the Java installs are maintained.

brew tap caskroom/cask
brew tap caskroom/versions

// we will also install the handy cask completions.
brew install brew-cask-completion

We can check the various releases of Java that are available.

brew cask info java // checks latest
brew cask info java7
brew cask info java8

Let's install the latest release of Java 8. The following will download the JDK release, unpack it and install it. You will probably be prompted to enter your password since the install will require administrator privileges.

brew cask install java8

Install other versions of Java

Let's add the latest version of Java in addition to 8.

brew cask install java

Check what JDKs we have installed now.

ls -la /Library/Java/JavaVirtualMachines/

total 0
drwxr-xr-x  4 root  wheel  128  7 Sep 11:04 .
drwxr-xr-x  6 root  wheel  192  7 Sep 11:04 ..
drwxr-xr-x  3 root  wheel   96  7 Sep 11:04 jdk-10.0.2.jdk
drwxr-xr-x  3 root  wheel   96  6 Sep 17:35 jdk1.8.0_181.jdk

So I have 8 and 10 installed. What is the default?

java -version

java version "10.0.2" 2018-07-17
Java(TM) SE Runtime Environment 18.3 (build 10.0.2+13)
Java HotSpot(TM) 64-Bit Server VM 18.3 (build 10.0.2+13, mixed mode)

Install Jenv

Now I want a convenient way to switch the default version. Jenv is a useful utility to help with this.

brew install jenv

Then add the following line to your ~/.bash_profile which will initialize Jenv whenever you start up a shell.

if which jenv > /dev/null; then eval "$(jenv init -)"; fi

You can reload your profile to make the addition effective with source ~/.bash_profile

We need to tell Jenv about each JDK we have (your JDK version will obviously differ).

jenv add /Library/Java/JavaVirtualMachines/jdk1.8.0_181.jdk/Contents/Home/
jenv add /Library/Java/JavaVirtualMachines/jdk-10.0.2.jdk/Contents/Home/

To check what versions Jenv can see now.

jenv versions

* system (set by /Users/jjack/.jenv/version)
  1.8.0.181
  10.0.2
  oracle64-1.8.0.181
  oracle64-10.0.2

Using Jenv

To configure the global version on the machine.

jenv global 1.8.0.181

Jenv also allows you to configure local version (per directory).

jenv local 10.0.2

Or even per shell instance.

jenv shell 1.8.0.181
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment