Managing Multiple version of JDK on Ubuntu

Posted By : Kapil Bharadwaj | 17-Jul-2017

if you want to install multiple versions of JDK on your machine but wondering whether the last installed version would overwrite the previous version ? well it will not overwrite the previous JDK version.so you can install multiple versions of JDK on your ubuntu server but after installing multiple versions on single ubuntu server,you need to switch them according to your needs.for example assuming you have both JDK7 and JDK8 installed on your system and one of your project is dependent on JDK 7 and other is dependent on JDK 8. so to run the project you have to switch to JDK version according to your current working project.
first let us install the two different versions of JDKs on our system.
#type the command on the terminal to install JDK 7:

$ sudo apt-get install openjdk-7-jdk

you may ask to enter password.so enter password to finish the installation.
after installation gets complete,you can confirm it by typing in the terminal:

$ java -version 

it will show the version of JDK which you just installed.

now we have to install other version of JDK.
# Install JDK 8 by command in terminal:

$ sudo apt-get install openjdk-8-jdk    

now we have both JDK 7 and JDK 8 installed on our system.
if you enter java -version on terminal,it will give only default version of JDK not the both.
so to check both installed versions of JDK,enter following command on terminal:

 $ update-java-alternatives --list 

 

it will give you both versions of JDK like this:

java-1.7.0-openjdk-amd64 1071 /usr/lib/jvm/java-1.7.0-openjdk-amd64

java-1.8.0-openjdk-amd64 1069 /usr/lib/jvm/java-1.8.0-openjdk-amd64 

now if you want to switch from the default version of JDK to your desired version, enter the following command on terminal:

 

$ sudo update-alternatives --config java 

 

after entering the command you will get the list indicating the current JDK version by prefixing * on it.
here below in our case it is JDK 8 which is set by default. after entering above command you will get result like this: 


There are 2 choices for the alternative java (providing /usr/bin/java).

  Selection    Path                                            Priority   Status
------------------------------------------------------------
  0            /usr/lib/jvm/java-7-openjdk-amd64/jre/bin/java   1071      auto mode
  1            /usr/lib/jvm/java-7-openjdk-amd64/jre/bin/java   1071      manual mode
* 2            /usr/lib/jvm/java-8-openjdk-amd64/jre/bin/java   1069      manual mode

Press enter to keep the current choice[*], or type selection number:

 

now you can enter the number of the JDK version which you want to switch to or just press enter key if you want to keep the current one.

 

that's all now you can switch to any installed version of JDK whenever you want.

About Author

Author Image
Kapil Bharadwaj

Kapil is a Node Js Developer.

Request for Proposal

Name is required

Comment is required

Sending message..