How to set default Java version in Ubuntu

The Ubuntu archives have multiple versions of OpenJDK available. One of these is designated as the default and this has the package names default-jdk and default-jre. The java and javac programs will be symlinked to the binaries from this default JDK. On my Ubuntu, the default packages were linked to the openjdk-7-jdk and openjdk-7-jre packages.

However, you might want to install and use other versions of JDK. For example, to use Java 8 I did:

$ sudo apt install openjdk-8-jdk

The problem is that java, javac and other binaries still point to the default Java version. To switch the default Java binaries, use the update-java-alternatives tool.

  • To list the Java versions installed on your system, use the --list option:
$ update-java-alternatives --list
java-1.7.0-openjdk-amd64 1071 /usr/lib/jvm/java-1.7.0-openjdk-amd64
java-1.8.0-openjdk-amd64 1069 /usr/lib/jvm/java-1.8.0-openjdk-amd64
  • To set one of the above Java versions as the default, use the --set option:
$ sudo update-java-alternatives --set java-1.8.0-openjdk-amd64

Tried with: Ubuntu 14.04