Manage multiple JDK versions

Tue 30 March 2021

To find the available JDK packages available for your system you can use apt search, to install for example java-8 and java-11 JDK:

sudo apt install -y openjdk-8-jdk openjdk-11-jdk

NOTE: Since Debian 11, minimum available openjdk vesion included in the debian repositories, is version 11. The older JDK version can still be downloaded from: openlogic; here you can download the .deb package which can be installed with dpkg -i <package-name.deb>.

You should now have both java versions intalled at /usr/lib/jvm. To list the available versions on your system run:

sudo update-alternatives --config java

You should now see the following:

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

  Selection    Path                                            Priority   Status
------------------------------------------------------------
  0            /usr/lib/jvm/java-11-openjdk-amd64/bin/java      1111      auto mode
  1            /usr/lib/jvm/java-11-openjdk-amd64/bin/java      1111      manual mode
* 2            /usr/lib/jvm/java-8-openjdk-amd64/jre/bin/java   1081      manual mode

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

Usually simply selecting a java version from the provideded menu is sufficient, however in some situations a program might use an environment variable, e.g. JAVA_HOME, if this is the case, note that you need to update your .bashrc, i.e. provide export JAVA_HOME=/usr/lib/jvm/java-8-oracle. In this case you might want to use a script to quickly switch between java versions:

sudo update-java-alternatives -s java-8-oracle
export JAVA_HOME=/usr/lib/jvm/java-8-oracle/
export PATH=$PATH:$JAVA_HOME

Category: Debian Linux