Working with multiple Java versions

With ever growing use of Java in multi-platform applications, it is a common problem that different applications need different versions of Java. That means, we need to have multiple versions of Java installed on a system, and keep on switching between them.

But how? By adjusting the JAVA_HOME variable!!! This overall cumbersome looking task has been made quite trivial by using the Java environment manager (jenv) from Gildas Cuisinier. Overall installation & usage instructions on various platforms are given on its home page, as well as on its github repo. However, here’s a quick summary for Linux bash environment:

  • Download jenv in your desired location, say in the hidden .jenv directory under your home:
$ git clone https://github.com/jenv/jenv ~/.jenv
  • Add the jenv script in the execution path, basically add the following line in ~/.bash_profie or ~/.profile:
export PATH=${PATH}:~/.jenv/bin
  • Then, add jenv’s initialization, i.e. add the following line in ~/.bashrc:
eval "$(jenv init -)"
  • Now, logout and login back for all the settings to take effect. And after logging in, if the system has a pre-installed Java version,
$ jenv versions

should show one line with “* system (set by …”. For other Java version(s), we need to download and add.

  • In case, corresponding Java version is already downloaded & extracted, go to the next bullet. Otherwise, download the sources (.tar.gz or .bin) of various Java versions you need. Untar the .tar.gz or execute the .bin (after adding execute permissions to it). It would extract them in their corresponding folders. Shown below is one example of each.

Java Downloads

NB There are two variants of Java. One maintained by Oracle (called JDK, Java SE, …) and the other by open source community (called OpenJDK). Here are the corresponding links:

  1. JDK (Java SE, RE etc) is from Sun / Oracle (w/ Oracle Binary Code License)
  2. OpenJDK is the open source version of the above (w/ GNU General Public License version 2)
  • Now, time to add the downloaded & extracted Java version(s) using jenv add <extracted_java_folder>, e.g.
$ jenv add jdk1.6.0_45/
$ jenv add java-se-7u75-ri/

NB One may give the complete or relative path of the extracted folders. After this, the

$ jenv versions

should show something like this:

Java Versions

  • Now, switch to the desired Java version from the list using any of the following:
$ jenv local <version>

for only this current directory.

$ jenv shell <version>

for only this particular shell.

$ jenv global <version>

for globally.

$ jenv versions

should show the “*” (current setting) shifted appropriately.

Anil Kumar Pugalia (123 Posts)

The author is a hobbyist in open source hardware and software, with a passion for mathematics, and philosopher in thoughts. A gold medallist from the Indian Institute of Science, Linux, mathematics and knowledge sharing are few of his passions. He experiments with Linux and embedded systems to share his learnings through his weekend workshops. Learn more about him and his experiments at https://sysplay.in.


   Send article as PDF   

Leave a Reply

Your email address will not be published. Required fields are marked *