Last Updated: October 04, 2020
·
796
· sappo

Switch java-jdk on ubuntu

You maybe experiencing that handling multiple java jdk version is a bit of a pain under ubuntu. If you got openjdk installed and want to switch to Oracle jdk my only take usually was to completely uninstall openjdk. But there's an alternative.

Ubuntu has a tool called update-alternative which allows you to register and switch tool like java and javac. Openjdk will install itself and correctly setup as an alternative. For Oracle jdk to is a bit more complicated.

We start by downloading the jdk from oracle and extracting the binaries to "/opt" for example.
Then we are registering java, javac, javaws and the java firefox plugin to the Oracle jdk as an alternative.


$ sudo update-alternatives --install "/usr/bin/java" "java" "/opt/jdk1.x.x_xx/bin/java" 1
$ sudo update-alternatives --install "/usr/bin/javac" "javac" "/opt/jdk1.x.x_xx/bin/javac" 1
$ sudo update-alternatives --install "/usr/lib/mozilla/plugins/libjavaplugin.so" "mozilla-javaplugin.so" "/opt/jdk1.x.x_xx/jre/lib/amd64/libnpjp2.so" 1
$ sudo update-alternatives --install "/usr/bin/javaws" "javaws" "/opt/jdk1.x.x_xx/bin/javaws" 1

Using the following command we can choose between our alternatives


$ sudo update-alternatives --config java
$ sudo update-alternatives --config javac

More details at https://help.ubuntu.com/community/Java

Related protips:

How To Check If JDK Is 32-bit or 64-bit?