DEV Community

Johnny Boy
Johnny Boy

Posted on • Updated on

Setup your MacOS for Java Development with multiple Java Versions

Disclaimer

This is my personal preference on how to setup my local environment for Java development with different Java Versions.
Feel free to leave your suggestions if you think there is a better setup.

Getting a new Terminal

I've used iTerm2 for some years now, and I've found it pretty useful.
You can download it here in its own SITE.
After you have downloaded the zip file, you just installed, for more info you can check their FAQs.

Grabbing your Favorite Package Manager

If you have been using MacOS for a while, by now you should be familiar with Hombebrew.
To install it you can just run this command:

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

Or if you prefer, you can go to their page and check the steps there.

Homebrew already installed?

In case you have installed homebrew some time before.
Remember to update your formulas with the next command:

$ brew update

Improving your shell experience

I've used ZSH and Oh My Zsh for some time. And you can install them by running this two commands.

$ brew install zsh
$ sh -c "$(curl -fsSL https://raw.github.com/ohmyzsh/ohmyzsh/master/tools/install.sh)"

If you want to know more about, check out the Oh My Zsh site.

Managing your Java environment

When you are developing with multiple services, it is fairly common that you find yourself in the situation on having multiple java versions in your local machine. The tool that I have used in the past is jEnv.
To install this great tool in your local machine, you just need to run the following command:

$ brew install jenv

After you have installed jenv you need to make sure that this is available every time you start your computer, to do this you will need to update your ~/.zshrc with this next commands:

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

Knowing & installing the Java versions you want

To know which versions of java you have available in homebrew you can run this:

$ brew search java

At this point in time we have available 3 java versions: java, java11 and java6. The first java formula is the latest.
To install these three java versions, you can run these commands:

$ brew cask install java
$ brew cask install java11
$ brew cask install java6

After installing this, you need to add them to the available jenv versions.

## First create the versions folder.
$ mkdir ~/.jenv/versions
## Add the versions to jenv
$ jenv add /Library/Java/JavaVirtualMachines/openjdk-13.0.2.jdk/Contents/Home
$ jenv add /Library/Java/JavaVirtualMachines/openjdk-11.0.2.jdk/Contents/Home
$ jenv add /Library/Java/JavaVirtualMachines/1.6.0.jdk/Contents/Home

Extra commands

To make easier to change the java version I use some aliases, by running these next commands you run:

$ echo 'alias refreshZsh="source ~/.zshrc"' >> ~/.zshrc
$ echo 'alias goJava13="jenv global 13.0 && refreshZsh"' >> ~/.zshrc
$ echo 'alias goJava11="jenv global 11.0 && refreshZsh"' >> ~/.zshrc
$ echo 'alias goJava6="jenv global 1.6 && refreshZsh"' >> ~/.zshrc

After this you can run:

$ source ~/.zshrc

Installing Gradle

You can check the official documentation from Gradle on how to install it with a package manager here.
Or simply running this command:

$ brew install gradle

Test your environment

We can test this configuration by cloning this repo from SpringBoot:

$ git clone https://github.com/spring-guides/gs-rest-service.git
$ cd gs-rest-service/complete
$ gradle bootRun

By now, you should see the spring ascii art in the console, and you should have accessible: http://localhost:8080/greeting

Extra

Nowadays if you are starting a new project you might want to consider using Kotlin, to install it you can just run:

$ brew install kotlin

Success!

Now you should be able to configure multiple java versions in your local computer.

Top comments (1)

Collapse
 
scottshipp profile image
scottshipp

Yeah this is pretty much what I do, except I use SDKMan! Do you know it? More info about SDKMan! and the other tools I use is here.