Code Yarns ‍👨‍💻
Tech BlogPersonal Blog

How to set default Java version in Ubuntu

📅 2015-Dec-01 ⬩ ✍️ Ashwin Nanjappa ⬩ 🏷️ jdk, jre, openjdk ⬩ 📚 Archive

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-11-jdk and openjdk-11-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.

$ update-java-alternatives --list
java-1.11.0-openjdk-amd64      1111       /usr/lib/jvm/java-1.11.0-openjdk-amd64
java-1.8.0-openjdk-amd64       1081       /usr/lib/jvm/java-1.8.0-openjdk-amd64
$ sudo update-java-alternatives --set java-1.8.0-openjdk-amd64
$ cd /usr/lib/jvm

$ ls

$ sudo rm -f default-java

$ sudo ln -s java-1.8.0-openjdk-amd64 default-java

Tried with: Ubuntu 18.04