Switch Java Version with update-alternatives

In Debian/Unbutu, how to install different version of java and use update-alternatives/update-java-alternatives to switch different version of java

In Linux, like Debian or Ubuntu system, want to install different version of java and can swith system java version easily, the easiest solution is use update-alternatives.

update-alternatives creates, removes, maintains and displays information about the symbolic links comprising the Debian alternatives system.

How update-alternatives works

It is possible for several programs fulfilling the same or similar functions to be installed on a single system at the same time. For example, many systems have several text editors installed at once. This gives choice to the users of a system, allowing each to use a different editor, if desired, but makes it difficult for a program to make a good choice for an editor to invoke if the user has not specified a particular preference.

Debian’s alternatives system aims to solve this problem. A generic name in the filesystem is shared by all files providing interchangeable functionality. The alternatives system and the system administrator together determine which actual file is referenced by this generic name. For example, if the text editors ed(1) and nvi(1) are both installed on the system, the alternatives system will cause the generic name /usr/bin/editor to refer to /usr/bin/nvi by default. The system administrator can override this and cause it to refer to /usr/bin/ed instead, and the alternatives system will not alter this setting until explicitly requested to do so.

The generic name is not a direct symbolic link to the selected alternative. Instead, it is a symbolic link to a name in the alternatives directory, which in turn is a symbolic link to the actual file referenced. This is done so that the system administrator’s changes can be confined within the /usr/local/etc directory.

Where is java

In Debian/Ubunut, java is symbol link in /bin/java which point to /etc/alternatives/java, which is another symbol link, it point to the current java path.

# which java

# ls -l /bin/java
lrwxrwxrwx 1 root root 22 Mar 25 20:53 /bin/java -> /etc/alternatives/java

# ls -l /etc/alternatives/java
lrwxrwxrwx 1 root root 46 Apr  7 09:07 /etc/alternatives/java -> /usr/lib/jvm/java-8-openjdk-arm64/jre/bin/java

List installed java versions

Use update-alternatives --list java to list all installed java paths. Example:

# update-alternatives --list java

Switch java version with update-alternative

Use update-alternatives --config java to update java version in interactive mode. Example:

# update-alternatives --config java
There are 2 choices for the alternative java (providing /usr/bin/java).

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

Press <enter> to keep the current choice[*], or type selection number: 2
update-alternatives: using /usr/lib/jvm/java-8-openjdk-arm64/jre/bin/java to provide /usr/bin/java (java) in manual mode

# java -version
openjdk version "1.8.0_312"
OpenJDK Runtime Environment (build 1.8.0_312-8u312-b07-0ubuntu1~20.04-b07)
OpenJDK 64-Bit Server VM (build 25.312-b07, mixed mode)

When using the --config option, update-alternatives will list all of the choices for the link group of which given name is the master alternative name. The current choice is marked with a *. You will then be prompted for your choice regarding this link group. Depending on the choice made, the link group might no longer be in auto mode. You will need to use the --auto option in order to return to the automatic mode (or you can rerun --config and select the entry marked as automatic).

If you want to configure non-interactively you can use the --set option instead. For example, in script you donot want user input. You can use update-alternatives --set java <path> to update java version directly. Example:

# update-alternatives --set java /usr/lib/jvm/java-8-openjdk-arm64/jre/bin/java
update-alternatives: using /usr/lib/jvm/java-8-openjdk-arm64/jre/bin/java to provide /usr/bin/java (java) in manual mode
root@9b816ba2e3cb:/project# java -version
openjdk version "1.8.0_312"
OpenJDK Runtime Environment (build 1.8.0_312-8u312-b07-0ubuntu1~20.04-b07)
OpenJDK 64-Bit Server VM (build 25.312-b07, mixed mode)

Use update-java-alternatives to update java version

Alternative, a dedicate command update-java-alternatives can also be used to switch java version.

update-java-alternatives updates all alternatives belonging to one runtime or development kit for the Java language. A package does provide these information of it’s alternatives in /usr/lib/jvm/.<jname>.jinfo.

Use -l to list available java version example:

# update-java-alternatives -l
java-1.11.0-openjdk-arm64      1111       /usr/lib/jvm/java-1.11.0-openjdk-arm64
java-1.8.0-openjdk-arm64       1081       /usr/lib/jvm/java-1.8.0-openjdk-arm64

Use -s to set java version to Java 11 example:

# update-java-alternatives -s java-1.11.0-openjdk-arm64
update-alternatives: error: no alternatives for

# java -version
openjdk version "11.0.14" 2022-01-18
OpenJDK Runtime Environment (build 11.0.14+9-Ubuntu-0ubuntu2.20.04)
OpenJDK 64-Bit Server VM (build 11.0.14+9-Ubuntu-0ubuntu2.20.04, mixed mode)


To get JAVA_HOME, use java -XshowSettings:properties -version to print all current java property settings and look for java.home. Example:

# java -XshowSettings:properties -version
Property settings:
    awt.toolkit = sun.awt.X11.XToolkit
    file.encoding = UTF-8
    file.separator = /
    java.awt.graphicsenv = sun.awt.X11GraphicsEnvironment
    java.awt.printerjob = sun.print.PSPrinterJob
    java.class.path =
    java.class.version = 55.0
    java.home = /usr/lib/jvm/java-11-openjdk-arm64 = /tmp
    java.library.path = /usr/java/packages/lib
        /usr/lib = OpenJDK Runtime Environment
    java.runtime.version = 11.0.14+9-Ubuntu-0ubuntu2.20.04 = Java Platform API Specification
    java.specification.vendor = Oracle Corporation
    java.specification.version = 11
    java.vendor = Ubuntu
    java.vendor.url =
    java.vendor.url.bug =
    java.version = 11.0.14 = 2022-01-18
    java.vm.compressedOopsMode = 32-bit = mixed mode = OpenJDK 64-Bit Server VM = Java Virtual Machine Specification
    java.vm.specification.vendor = Oracle Corporation
    java.vm.specification.version = 11
    java.vm.vendor = Ubuntu
    java.vm.version = 11.0.14+9-Ubuntu-0ubuntu2.20.04
    jdk.debug = release
    line.separator = \n
    os.arch = aarch64 = Linux
    os.version = 5.10.0-12-arm64
    path.separator = : = 64
    sun.boot.library.path = /usr/lib/jvm/java-11-openjdk-arm64/lib
    sun.cpu.endian = little
    sun.cpu.isalist = = UnicodeLittle = SUN_STANDARD
    sun.jnu.encoding = UTF-8 = HotSpot 64-Bit Tiered Compilers
    sun.os.patch.level = unknown = US
    user.dir = /project
    user.home = /root
    user.language = en = root
    user.timezone =

openjdk version "11.0.14" 2022-01-18
OpenJDK Runtime Environment (build 11.0.14+9-Ubuntu-0ubuntu2.20.04)
OpenJDK 64-Bit Server VM (build 11.0.14+9-Ubuntu-0ubuntu2.20.04, mixed mode)

To get JAVA_HOME, check the java.home property. Example:

# java -XshowSettings:properties -version 2>&1 |grep java.home
    java.home = /usr/lib/jvm/java-11-openjdk-arm64

Use grep and awk to extract value of java.home and set to JAVA_HOME. Example:

# java -XshowSettings:properties -version 2>&1 |grep java.home | awk -F= '{print $2}'

# JAVA_HOME=$(java -XshowSettings:properties -version 2>&1 |grep java.home | awk -F= '{print $2}')

Install different version of java

First list available java version in apt. Example:

# apt-cache search openjdk | grep '[-]jdk '
openjdk-11-jdk - OpenJDK Development Kit (JDK)
openjdk-8-jdk - OpenJDK Development Kit (JDK)
openjdk-13-jdk - OpenJDK Development Kit (JDK)
openjdk-16-jdk - OpenJDK Development Kit (JDK)
openjdk-17-jdk - OpenJDK Development Kit (JDK)

Then just install by apt-get, Example:

apt-get install -y openjdk-17-jdk


update-alternatives: command not found

When run update-alternatives, got command not found error:

# update-alternatives
bash: update-alternatives: command not found

update-alternatives is available in apt, use apt-get install to install:

apt-get install -y update-alternatives


OmniLock - Block / Hide App on iOS

Block distractive apps from appearing on the Home Screen and App Library, enhance your focus and reduce screen time.

DNS Firewall for iOS and Mac OS

Encrypted your DNS to protect your privacy and firewall to block phishing, malicious domains, block ads in all browsers and apps