How to Properly Set JDK 8 as Default Java Version on Debian 8 with Alternatives System


2 views

When working with Debian 8 (Jessie), many developers encounter a frustrating situation where Java tools stubbornly remain linked to JDK 7 even after attempting to set JDK 8 as the default through update-java-alternatives. The core issue lies in how Debian's alternatives system manages Java toolchains.

The error about missing mozilla-javaplugin.so is indeed just a warning - Java 8 moved away from the IcedTea browser plugin architecture. However, the real problem appears when checking individual tool links:

# update-alternatives --get-selections | grep java
javac                          auto     /usr/lib/jvm/java-7-openjdk-amd64/bin/javac
javadoc                        auto     /usr/lib/jvm/java-7-openjdk-amd64/bin/javadoc

While the brute-force approach works, there's a more elegant solution using Debian's alternatives system more effectively:

# First verify available Java installations
ls /usr/lib/jvm

# Set the default Java runtime
sudo update-alternatives --config java

# Set the default Java compiler
sudo update-alternatives --config javac

# For complete JDK tools synchronization
sudo update-alternatives --set javac /usr/lib/jvm/java-8-openjdk-amd64/bin/javac
sudo update-alternatives --set javadoc /usr/lib/jvm/java-8-openjdk-amd64/bin/javadoc

For those managing multiple systems, here's a script to automate the transition:

#!/bin/bash
JAVA8_PATH="/usr/lib/jvm/java-8-openjdk-amd64"

for tool in $(update-alternatives --get-selections | grep java | awk '{print $1}')
do
  if [ -f "${JAVA8_PATH}/bin/${tool}" ]; then
    sudo update-alternatives --set "${tool}" "${JAVA8_PATH}/bin/${tool}"
  elif [ -f "${JAVA8_PATH}/jre/bin/${tool}" ]; then
    sudo update-alternatives --set "${tool}" "${JAVA8_PATH}/jre/bin/${tool}"
  fi
done

After making changes, always verify with:

java -version
javac -version
update-alternatives --get-selections | grep java

The issue stems from how Debian's package manager handles Java installations. The java-common package manages the alternatives system, but some tools maintain their own symlinks. When mixing installations from backports with main repository packages, these inconsistencies can occur.


When attempting to switch from Java 7 to Java 8 as the default JDK on Debian 8 using update-java-alternatives, you may encounter two issues:

update-alternatives: error: no alternatives for mozilla-javaplugin.so
update-java-alternatives: plugin alternative does not exist: /usr/lib/jvm/java-8-openjdk-amd64/jre/lib/amd64/IcedTeaPlugin.so

Additionally, many Java tools might still point to Java 7 despite running the update command.

The IcedTea plugin warning is harmless for most development purposes. Since JDK 8 doesn't include the IcedTea browser plugin (which was removed for security reasons), this error can be safely ignored unless you specifically need Java browser plugin support.

Here's the most reliable method to switch all Java alternatives to JDK 8:

sudo update-alternatives --set java /usr/lib/jvm/java-8-openjdk-amd64/jre/bin/java
sudo update-alternatives --set javac /usr/lib/jvm/java-8-openjdk-amd64/bin/javac

For a complete switch of all Java-related tools, use either of these approaches:

Method 1: Interactive Selection

for tool in $(update-alternatives --get-selections | grep java | awk '{print $1}'); do
    sudo update-alternatives --config $tool
done

Method 2: Non-interactive (for scripting)

for tool in $(update-alternatives --get-selections | grep java | awk '{print $1}'); do
    sudo update-alternatives --set $tool /usr/lib/jvm/java-8-openjdk-amd64/bin/${tool}
done

After making changes, verify them with:

java -version
javac -version
update-alternatives --get-selections | grep java

Create a simple script to handle JDK version switching:

#!/bin/bash
# jdk-switch.sh
TARGET_JDK=$1
JDK_PATH="/usr/lib/jvm/${TARGET_JDK}"

if [ ! -d "${JDK_PATH}" ]; then
    echo "JDK path ${JDK_PATH} not found"
    exit 1
fi

for tool in $(update-alternatives --get-selections | grep java | awk '{print $1}'); do
    if [ -f "${JDK_PATH}/bin/${tool}" ]; then
        sudo update-alternatives --set ${tool} "${JDK_PATH}/bin/${tool}"
    elif [ -f "${JDK_PATH}/jre/bin/${tool}" ]; then
        sudo update-alternatives --set ${tool} "${JDK_PATH}/jre/bin/${tool}"
    fi
done

Usage: sudo ./jdk-switch.sh java-8-openjdk-amd64

The update-java-alternatives command sometimes misses certain symbolic links because:

  • Not all Java tools are registered in the same way
  • The plugin-related errors can cause premature termination
  • Some tools might have different paths in different JDK versions