Setting up a Mac for Java Development

Michael Pellaton
Netcetera Tech Blog
6 min readJan 3, 2018

--

This post is a collection of personal notes from the endless journey to find the perfect set-up of an Apple Mac as Java development system.

The personal computer is the most important physical piece of equipment for software developers and the countless hours we spend with it legitimate a well engineered, easy to use and ergonomic setup of the development tools. Please bear in mind that such a setup consist to a large extent of personal choices and that this article represents just the choices made by the author.

Source: Pexels.com

Source Code Font

As software developers we gaze at text on our screens for hours on end and choosing an easy to read text font for the code and the console helps to avoid fatigue. Additionally to the monospaced fonts that come as part of macOS, there are some freely available fonts especially designed for source code, like Hack [1] or Source Code Pro [2].

Monaco and Andale Mono (both included in macOS), Hack [1] and Source Code Pro [2]

Once you settled on a source code text font, configure it in the Terminal and all code editors.

Terminal Configuration

Transparency
We found the terminal window’s slight transparency set by default to be distracting, especially when other terminal windows behind contain moving content like build output. Set the opacity to 100% as a remedy.

Color Scheme (Profile)
To bring the terminal experience on the Mac a bit closer to what is known from Linux, we recommend the dark «Pro» profile tweaked with the font of your choice and a few of the colors replaced with slightly brighter ones.

Bash configuration

Colored Bash
Edit the file ~/.bash_profile (or create it if there is none) and add the following line to enable colored output in the Bash:

export CLICOLOR=1

Prompt
Have a look at the manifold configuration possibilities [3] of the Bash prompt and choose what you want to see and how it should be formatted. The «Easy Bash Prompt Generator» [4] offers drag & drop and gives an instant preview. Once you decided on your prompt, add the export PS1=...statement to the ~/.bash_profile file. The example below shows the prompt of the author:

export PS1=”\[\033[01;32m\]\u@\h\[\033[00m\]:\[\033[01;34m\]\w\[\033[00m\]\$ “
Example prompt and coloured Bash output

Aliases

Frequent Linux users might be used to comfort aliases like ll, la, and ... Add the following lines to the file ~/.bash_profile to have them on the Mac:

alias ll=’ls -lh’
alias la=’ls -lah’
alias latr=’ls -lahtr’
alias cd..='cd ..'
alias ..='cd ..'

Homebrew

Homebrew [5] is a package manager for macOS. To install it run the following command, all on one line, in a terminal:

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

Version Control Systems

macOS comes with Git and Subversion on board, but they are often quite dated. To install the latest stable versions using Homebrew, run the command brew install git svn.

To see the difference, run git --version or svn --version before and after the installation.

Git Configuration
Git needs a user name and email to work properly. If unconfigured, it will derive them from the local user and computer names, which is almost certainly wrong. To set them, run the following commands in a terminal (substituting the author’s sample values):

git config --global user name Michael Pellaton
git config --global user email michael.pellaton@somedomain.ch

For more configuration options, please refer to the Git documentation [6] and to be able to work with GitHub repositories, follow their set-up guide [7].

This is just the very basic Git configuration that will most probably not suffice in practice. An approach to a Git configuration for a more real-world scenario is the topic of an another blog post of mine [8].

Build tools

To install the Java build tools Maven and/or Gradle, run the command brew install maven gradle in a terminal.

Java Development Kit (JDK)

The straight forward way to get a JDK onto a Mac is a download from Oracle [9] followed by the guided setup using the installer. While this method might be sufficient for some simple cases, it lacks the flexibility developers need. For example, it’s impossible to install an older JDK version once a newer one is present on the system. Further, there is no easy way to switch the default when multiple versions are installed, and it is not easily possible to install a new JDK to have it available as development target, but use the older version as system default to run the build tools.

The setup described below offers all this flexibility and it even adds some tooling to ease version switching.

Cask

Homebrew Cask [10] is an extension for Homebrew to install and manage applications that come with an interactive installer (like the JDK). To install and configure Cask, run the following commands in a terminal:

brew install brew-cask
brew tap caskroom/versions
echo alias cask=”’brew cask’” >> ~/.bash_profile
source ~/.bash_profile

The «unofficial» alias cask created above saves you from typing brew cask.

jEnv

jEnv [11] is a command line tool that helps managing multiple Java versions on a system, much like the Debian Alternatives System [12]. To install and configure jEnv, run the following commands in a terminal:

brew install jEnv
echo ‘export PATH=”$HOME/.jenv/bin:$PATH”’ >> ~/.bash_profile
echo ‘eval “$(jenv init -)”’ >> ~/.bash_profile
jenv enable-plugin export
source ~/.bash_profile

JDK Installation

Use Cask to find and install the Java versions wanted:

  • List all available Java versions: cask search java
  • Get detailed information about a package: cask info java
  • Install Java 6, 8 and 9: cask install java6 java8 java
  • List installed Cask packages: cask list
  • Uninstall Java 6: cask uninstall java6

Configuration and Usage of jEnv

  • Register the JDKs installed with Cask with jEnv: jenv add JDK_HOME_PATH
    JDKs are installed in /Library/Java/JavaVirtualMachines/jdk-VERSION/Contents/Home.
    So, a full example would be:
    jenv add /Library/Java/JavaVirtualMachines/jdk-9.0.1/Contents/Home/
  • List all JDKs known to jEnv: jenv versions
    Please note that each JDK might be registered under various version names. jEnv derives these version names from java -version.
  • Set the global Java version: jenv global 9.0.1
  • List the global Java version: jenv global

Besides the global Java version, jEnv also supports directory and shell local settings. As these topics would go well beyond the scope of this article, we refer to the corresponding documentation [13].

Demo Time: A Walk-Through

Instead of a live demo, the following sequences from a Bash session show how some of the tools and configurations explained in this article work:

The starting position: Java 9.0.1 as default
Setting the installed Java 8 as default and checking

Conclusion

At this point, the most important version control systems and build tools are in place and on top of that JDKs can be installed and managed with ease.
It is now left to the reader to install higher level tools like IDEs and profilers.

--

--