Juggling with many JDK and Scala versions under OS X

I’m always trying to be up-to-date with my developing environment, but often I need to use an older version of some particular component.
In this blogpost I’ll show you not only how to juggle with many versions of Scala and JDK, but also how to do this quickly and without any headaches.

Switching between Scala versions

I’ve installed scala using Homebrew.
This tool provides an easy way to install and keep up-to-date 3rd party software (with its dependencies) and allows you to rollback into previously installed version, using one simple command:

For example:

Note that this tip can be used not only in case of Scala – You can do that with anything installed with Homebrew.

What versions of Scala do I have?

You have to explicitly pass concrete versions as a last argument of brew switch.
But how can you check what versions you’ve already installed?
With brew it’s a piece of cake:

Currently used version is marked marked with asterisk at the end of line.

Switching between JDK versions

Few weeks ago I’ve decided to play with Java 8 and I had to upgrade JDK.
Unfortunately, some projects that I’m occasionally contributing to requires older development kit.

Luckily, official documentation brings some solution: http://docs.oracle.com/javase/7/docs/webnotes/install/mac/mac-jdk.html#version

What versions of JDKs do I have?

To list all JDKs available in your system, just type:

So far, so good.

But solution presented in official documentation seems to suppose that we’ll be able to replace each invocation of (for example) javac with something like /usr/libexec/java_home -v version --exec javac.

Switching between JDKs

Sounds strange to me, especially in a world of mavens, sbts and other build tools.. Fortunately there is an elegant solution: we can override environment variable called JAVA_HOME:

Let’s check if this works:

Remember that this trick will work only in a scope of single terminal session. So if you have many openend terminals, then you have to override JAVA_HOME in each of them.

You can create Bash alias for this export statement to extend you keyboard life or/and append its invocation to your ~/.profile / ~/.bashrc scripts if you want to change default version globally.


Before I found this solutions I was very afraid about upgrading my environment’s components, especially JDK.
However, as you could see, juggling with many Scala & Java versions isn’t so scary. Quite the opposite – it’s easier than you may expect 🙂

All kinds of feedback will be welcome.
Please note that english isn’t my native language and everyday I’m trying to improve my skills. Therefore I’ll be thankfull for any suggestions about my grammar and vocabulary.


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s