Tip: Manage multiple JVMs in Mac with Homebrew

I started my journey to learn Mulesoft Development Fundamentals using Mule 4, which currently supports only OpenJDK 1.8. Current version of java is 12. Thought it might be useful to share how to manage multiple jdk versions in Mac. You don\’t want to downgrade your default jdk to 8 as it might impact other applications.

Simplest way to manage is to use a package manager and my choice in Mac is Homebrew. Quoting Homebrew: \”Homebrew installs the stuff you need that Apple (or your Linux system) didn’t.\” Run this from the terminal to install Homebrew,

/bin/bash -c \"$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install.sh)\"

Next we have to install cask, which is basically list of repository for different software, called formula. Once you install the case you will be ready to install any software it supports. If you already have homebrew installed, but did not use it for sometime, it is always better to update it. Run this command from terminal to install cask and update brew if needed,

brew update && brew upgrade && brew tap homebrew/cask-versions && brew cleanup && brew cask cleanup

To install Open Jdk 8 run this command from the terminal,

brew cask install adoptopenjdk/openjdk/adoptopenjdk8

Look for all installed Java versions

ls /Library/Java/JavaVirtualMachines/

In my case this is the output,

adoptopenjdk-8.jdk	jdk-12.0.1.jdk		jdk-9.0.4.jdk		jdk1.8.0_151.jdk	jdk1.8.0_181.jdk

Install Jenv to manage java environments. Run this from the terminal,

brew install jenv

Add JVMs to jenv,

$ jenv add /Library/Java/JavaVirtualMachines/adoptopenjdk-8.jdk/Contents/Home/

$ jenv add /Library/Java/JavaVirtualMachines/jdk-12.0.1.jdk/Contents/Home/

$ jenv add /Library/Java/JavaVirtualMachines/jdk-9.0.4.jdk/Contents/Home/

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

Initially, Jenv will be using the system installed version of Java. Lets set the one we need. Before we proceed you may check all the installed version in Jenv using this command,

jenv versions

It should show all the installed version and the current global version highligted using a *

* system (set by /Users/username/.jenv/version)
  1.8
  1.8.0.181
  1.8.0.242
  12.0
  12.0.1
  9.0
  9.0.4
  openjdk64-1.8.0.242
  oracle64-1.8.0.181
  oracle64-12.0.1
  oracle64-9.0.4

Run this from the terminal to use Java 8 for all users,

jenv global openjdk64-1.8.0.242

Run this to set Java 8 for only current user,

jenv global openjdk64-1.8.0.242

Now jenv versions should return this,

  system
  1.8
  1.8.0.181
  1.8.0.242
  12.0
  12.0.1
  9.0
  9.0.4
* openjdk64-1.8.0.242 (set by /Users/username/.jenv/version)
  oracle64-1.8.0.181
  oracle64-12.0.1
  oracle64-9.0.4

Check Java version

java -version

It should return this,

openjdk version \"1.8.0_242\"
OpenJDK Runtime Environment (AdoptOpenJDK)(build 1.8.0_242-b08)
OpenJDK 64-Bit Server VM (AdoptOpenJDK)(build 25.242-b08, mixed mode)

Hope this tip helps for anyone who might need to manage multiple JVM versions.

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top