Installing Open JDK on Debian or Ubuntu Systems

Steps for installing OpenJDK on Debian-based systems.

Configure your operating system to use the latest version of OpenJDK 8.

Procedure

  1. Check which version of the JDK your system is using:
    java -version
    If Oracle Java is used, the results should look like:
    java version "1.8.0_221"
    Java(TM) SE Runtime Environment (build 1.8.0_221-b11)
    Java HotSpot(TM) 64-Bit Server VM (build 25.221-b11, mixed mode)
    If the OpenJDK is used, the results should look like:
    openjdk version "1.8.0_181"
    OpenJDK Runtime Environment (build 1.8.0_181-b13)
    OpenJDK 64-Bit Server VM (build 25.181-b13, mixed mode)
  2. Make a directory for the JDK:
    sudo mkdir -p /usr/lib/jvm
  3. Unpack the tarball and install the JDK:
    sudo tar zxvf jdk-8u221-linux-x64.tar.gz -C /usr/lib/jvm

    The JDK files are installed into a directory called /usr/lib/jvm/jdk-8u_version.

  4. Tell the system that there's a new Java version available:
    sudo update-alternatives --install "/usr/bin/java" "java" "/usr/lib/jvm/jdk1.8.0_version/bin/java" 1

    If updating from a previous version that was removed manually, execute the above command twice, because you'll get an error message the first time.

  5. Set the new JDK as the default:
    sudo update-alternatives --set java /usr/lib/jvm/jdk1.8.0_version/bin/java
  6. Make sure your system is now using the correct JDK. For example:
    java -version
    java version "1.8.0_221"
    Java(TM) SE Runtime Environment (build 1.8.0_221-b11)
    Java HotSpot(TM) 64-Bit Server VM (build 25.221-b11, mixed mode)