Switching Between Multiple JDKs in Ubuntu

You may work with multiple versions of JDK in your Ubuntu operating system. For switching between multiple JDKs in Ubuntu, you need to configure the java alternatives.

Switching between multiple versions of the java tool

Use the below command for Switching between multiple java tools in Ubuntu,

$ sudo update-alternatives --config java

For example, my computer has 3 JDKs:- java-13, jdk1.6, and jdk1.8

[email protected]:~$ java -version
java version "13.0.2" 2020-01-14
Java(TM) SE Runtime Environment (build 13.0.2+8)
Java HotSpot(TM) 64-Bit Server VM (build 13.0.2+8, mixed mode, sharing)


[email protected]:~$ sudo update-alternatives --config java
There are 3 choices for the alternative java (providing /usr/bin/java).

  Selection    Path                                  Priority   Status
------------------------------------------------------------
  0            /usr/lib/jvm/java-13-oracle/bin/java   1091      auto mode
* 1            /usr/lib/jvm/java-13-oracle/bin/java   1091      manual mode
  2            /usr/lib/jvm/jdk1.6.0_45/bin/java      1         manual mode
  3            /usr/lib/jvm/jdk1.8.0_231/bin/java     0         manual mode

Press <enter> to keep the current choice[*], or type selection number: 3
update-alternatives: using /usr/lib/jvm/jdk1.8.0_231/bin/java to provide
 /usr/bin/java (java) in manual mode


[email protected]:~$ java -version
java version "1.8.0_231"
Java(TM) SE Runtime Environment (build 1.8.0_231-b11)
Java HotSpot(TM) 64-Bit Server VM (build 25.231-b11, mixed mode)


[email protected]:~$ sudo update-alternatives --config java
There are 3 choices for the alternative java (providing /usr/bin/java).

  Selection    Path                                  Priority   Status
------------------------------------------------------------
  0            /usr/lib/jvm/java-13-oracle/bin/java   1091      auto mode
  1            /usr/lib/jvm/java-13-oracle/bin/java   1091      manual mode
  2            /usr/lib/jvm/jdk1.6.0_45/bin/java      1         manual mode
* 3            /usr/lib/jvm/jdk1.8.0_231/bin/java     0         manual mode

Press <enter> to keep the current choice[*], or type selection number: 0
update-alternatives: using /usr/lib/jvm/java-13-oracle/bin/java to provide
 /usr/bin/java (java) in auto mode


[email protected]:~$ java -version
java version "13.0.2" 2020-01-14
Java(TM) SE Runtime Environment (build 13.0.2+8)
Java HotSpot(TM) 64-Bit Server VM (build 13.0.2+8, mixed mode, sharing)
Switching between different JDKs in Ubuntu

Switching between multiple versions of javac tool

Switching between multiple JDKs in Ubuntu using the javac tool:- Like java command, we can also choose javac among multiple javac tools. Below command switch between the multiple javac tools.

$ sudo update-alternatives --config javac
$ javac -version
javac 1.8.0_231

$ sudo update-alternatives --config javac
There are 3 choices for the alternative javac (providing /usr/bin/javac).

  Selection    Path                                   Priority   Status
------------------------------------------------------------
  0            /usr/lib/jvm/java-13-oracle/bin/javac   1091      auto mode
  1            /usr/lib/jvm/java-13-oracle/bin/javac   1091      manual mode
  2            /usr/lib/jvm/jdk1.6.0_45/bin/javac      1         manual mode
* 3            /usr/lib/jvm/jdk1.8.0_231/bin/javac     0         manual mode

Press <enter> to keep the current choice[*], or type selection number: 0
update-alternatives: using /usr/lib/jvm/java-13-oracle/bin/javac to provide
 /usr/bin/javac (javac) in auto mode

$ javac -version
javac 13.0.2

By switching java or javac tools we can use different versions of javac and java tools.

Leave a Comment

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