As you might know Oracle has decided to make some money out of the JDK. We have two options basically. Oracle JDK LTS (Long Term Support), released every 3 years, which requires a commercial license or OpenJDK which is free but for bug fixes and security patches we should every 6 months upgrade it.

The latest JDK release at the time of writing this article is JDK 11.0.1. In recent years Java has changed a lot of its features and has not just deprecated packages but even removed them from JDK. You might feel the need to test the new features, migrate old projects but you might find cumbersome to set the Java environment every time you want to try a different JDK. Let’s see how we can make this process easier.

First Step — Let’s Brew it!

For those that doesn’t know Homebrew, according to their webpage, “Homebrew is the missing package manager for macOS” and “Homebrew installs the stuff you need that Apple didn’t.” — Bold.

So, it’s easy to install, just paste the following at a Terminal prompt:

We are going to use an extension (cask) of Homebrew that makes even easier to install applications in your Mac. According to their website:

“Homebrew Cask extends Homebrew and brings its elegance, simplicity, and speed to OS X applications and large binaries alike.

It only takes 1 line in your shell to reach 4085 Casks maintained by 414 contributors.” — Great! Why not!?

So, at your prompt:

brew tap caskroom/cask

This command will automatically update Homebrew before installing Cask.

Now you have your free MacOs package manager installed. Yey! For more details check Homebrew’s web page. (https://brew.sh/)

So, what’s next?

More tools — Got a utility belt?

The idea is to install multiple Java versions and we want to be able to switch from one to another easily. Of course you can do it manually, you have to open the browser, find the website, find the right link, wait the download to complete, set up your environment… but why would we want all of that, right? Let’s make our life easier, we can use another tool just for that, imagine switching versions with only one command. Imagined? Great, for that we have a great tool called jEnv!(applause).

According to jEnv’s web site (http://www.jenv.be/): “jEnv is a command line tool to help you forget how to set the JAVA_HOME environment variable”. — Ooh tempting!

Guess how we can install jEnv? Yes, with our great free package manager — Homebrew. Just at a terminal prompt:

brew install jenv

What about the JDK?

Great, we are well equipped with the right tools, now we can install our JDKs. By the way, there are other options of tools for doing the same as we are proposing here. We won’t look at them but if you are interested have a look on SDKMAN(https://sdkman.io) or Jabba(https://github.com/shyiko/jabba).

Let’s use once more Homebrew to do the hard work. Homebrew will install Java for us. Since October 2018, the OpenJDK version instead of Oracle JDK will be installed by Homebrew.

What’s the difference?

Some might not know but Oracle will discontinue old Java versions. That means there will be no bug or security fixes available and they will even suggest you to remove old versions.

Oracle has now, started at version 11, the LTS (Long term support) version of the JDK and you are going to need a licence if you want to use that.

OpenJDK is the free and open-source implementation of the Java Platform Standard Edition and it’s been the official reference implementation of Java SE since version 7. Since JDK 10 the effort to produce an open-source reference implementation of the Java SE Platform was moved over to the JDK Project. This project will keep users updated releasing new features every six months. So, we can say that things will get more dynamic.

Once again, Homebrew will install OpenJDK version of Java.

At a terminal prompt, to install the latest version, we just have to:

brew cask install java

For older versions we have to specify which one we want, for example:

brew cask install java10 brew cask install java9

Nice, you should now have different Java versions in your machine.

Final Step

Let’s solve a hide and seek sort of a problem. We basically need to tell jEnv where are the JDK versions we want it to manage, but where are they?

We can find them by running the following at your Terminal:

/usr/libexec/java_home -V

You should see something like this:

Matching Java Virtual Machines (3):

11.0.1, x86_64: “OpenJDK 11.0.1” /Library/Java/JavaVirtualMachines/openjdk-11.0.1.jdk/Contents/Home

1.8.0_121, x86_64: “Java SE 8” /Library/Java/JavaVirtualMachines/jdk1.8.0_121.jdk/Contents/Home

1.7.0_79, x86_64: “Java SE 7” /Library/Java/JavaVirtualMachines/jdk1.7.0_79.jdk/Contents/Home

Note that you can see the version details and its respective location.

Great, let’s add them to jEnv, yey!! But before that, you might want to close and open a new terminal prompt because jEnv won’t be completely installed if you don’t do that. If you try to add the library to jEnv and you get a message saying “… No such file or directory”, that’s the reason.

Got a fresh new terminal prompt? Now you can add the library you want by doing:

jenv add <jdk_path>

In my case for example I have run:

jenv add /Library/Java/JavaVirtualMachines/openjdk-11.0.1.jdk/Contents/Home jenv add /Library/Java/JavaVirtualMachines/jdk1.8.0_121.jdk/Contents/Home jenv add /Library/Java/JavaVirtualMachines/jdk1.7.0_79.jdk/Contents/Home

So I have added to jEnv 3 different versions of the JDK. We can check the versions jEnv is managing at your prompt by doing:

jenv versions

You might get something like this:

system

1.7

1.7.0.79

1.8

* 1.8.0.121 (set by /Users/brunofrascino/Documents/Projects/play/.java-version)

11.0

11.0.1

openjdk64–11.0.1

oracle64–1.7.0.79

oracle64–1.8.0.121

It seems that jEnv had already found my Java installations, note some duplicity of versions in there. You can remove the ones you don’t need at any time by using jEnv command “remove”:

jenv remove oracle64–1.7.0.79

Note that it won’t remove your Java installation but the link between it and jEnv.

Nice, but how do I use it?

You just basically call it defining a scope:

jenv <scope> <jdk-version>

Say you have many projects in your computer, you need to maintain different versions of Java. You can set a global Java version by doing for example:

jenv global 1.8.0.121

Now Java 8 rules. Check the java version by doing:

java -version java version “1.8.0_121”

Java(TM) SE Runtime Environment (build 1.8.0_121-b13)

Java HotSpot(TM) 64-Bit Server VM (build 25.121-b13, mixed mode)

Nice and clean, now what if you need a different version? You just have to do it again:

jenv global 11.0.1

And what if you need another version, you just have to do it again, and another version, again…

Nice right? But do you have to switch versions all the time you need to work on a different Java version project? You could, but you don’t need to. jEnv allows you to set a local environment. That means I can have for example a project1 folder and inside this folder I will be working with say Java 8, then I switch a project2’s folder where I will be working with say Java11. jEnv allows you to do that by setting a local scope. For example:

cd project1 jenv local 1.8.0.121

and:

cd project2 jenv local 11.0.1

Now every time you are inside project1's folder you will be using Java 8, and if you are inside project2’s folder you will be using Java 11, and if you are not inside any of these folders or any other with a local Java version setting, you will be using the version you have set globally. Flexible, nice and clean. Remember to check the version is in use with the Java command:

java -version.

That’s it! Now you are able to manage different Java environments in your MacOS. You can run your old Java legacy systems and also switch to your new bleeding edge new ones with just a few commands. You have just added flexibility and agility to your development environment. Enjoy!