If you’re working on multiple java projects, it is very common to switch between different java versions. I face this too often. We have a number of projects running in production which need different java version. Sometimes we need to fix issues or to add a customer required feature in older projects. These older projects require java version 6 or 7, while newer projects require java 8 or 9. So it is often required to switch between them during development.
There are many different ways to switch between java versions, most of them are dependent on different OS environments i.e. macOS, Linux, Windows etc. Below are some examples how to do the switching.
For ubuntu/debian linux:
$ sudo update-alternatives — config java
$ su $ /usr/sbin/alternatives — config java $ /usr/sbin/alternatives — config javac
$ /usr/libexec/java_home -v
When you work in multiple OS environments, i.e work in a server machine (linux) or in a desktop/laptop (macOS/ubuntu/windows), it is a problem to remember all these commands. So, is there any easier way to solve this problem?
Here comes SDKMAN! The Software Development Kit Manager. SDKMAN! is a tool for managing parallel versions of multiple Software Development Kits on most Unix based systems. It provides a convenient Command Line Interface (CLI) and API for installing, switching, removing and listing Candidates.
- SDKMAN! is for making life easier for developers. No more trawling download pages, extracting archives, messing with _HOME and PATH environment variables.
- It is written in bash and only requires curl and zip/unzip to be present on your system.
- It runs on any UNIX based platforms: Mac OSX, Linux, Cygwin, Solaris and FreeBSD. A Powershell CLI version is available for Windows users.
- It allows to manage multiple versions of each candidate.
To install this amazing tool, simply open a new terminal and enter:
$ curl -s "https://get.sdkman.io" | bash
Follow the instructions on-screen to complete installation.
It currently supports Ant, Gradle, Grails, Groovy, Java, Kotlin, Maven, sbt, Scala, Spark, Spring Boot, VisualVM and many more.
To list all the available candidates:
$ sdk list
Install the latest stable version of your SDK of choice (say, Java JDK) by running the following command:
$ sdk install java
Need a specific version of an SDK? Simply qualify the version you require:
$ sdk install java 9.0.4-oracle
Listing all available candidates for java:
$ sdk list java ================================================================================ Available Java Versions ================================================================================ 9.0.7-zulu + 9.0.4-oracle 9.0.4-openjdk 8.0.172-zulu > * 8.0.171-oracle 7.0.181-zulu + 7 10.0.1-zulu 10.0.1-oracle 10.0.0-openjdk ================================================================================ + - local version * - installed > - currently in use ================================================================================
When having multiple versions installed, to see the current version in use, run:
$ sdk current java
To switch the java version for the current terminal, run:
$ sdk use java 8.0.171-oracle
If you want to set a specific version as default for all terminals, run:
$ sdk default java 8.0.171-oracle
Now no matter what is your operating system is, you can easily switch between your SDK version running a common command.
For more detail about SDKMAN! go visit the official site here.
I must have to say, SDKMAN! is a must have tool for all software developers to make life easier managing different SDK versions. Give it a try. You will love it.