Here’s a handy howto on installing Oracle JDK™ 1.8 on modern Debian and Ubuntu distros.

Oracle does not provide *.deb packages or an APT repository for easy installation of their JDK, and it is a bit of a hassle to setup all the bits and pieces just right so that the Oracle JDK tools are properly integrated into a Debian or Ubuntu installation.

By following the steps below you’ll be able to switch between any installed JDKs, including OpenJDK versions, with a single command.

1) Download Oracle JDK

You can download Oracle JDK from this page. Look for the heading “Java SE Development Kit 8u102”, accept the license agreement and download the tarball file jdk-8u102-linux-x64.tar.gz. Update 102 was the latest at the time of writing this blog, it might be different when you read this. Remember to substitue the actual number everywhere.

Copy the file to the machine you want to install it on. We’ll assume the file is in /tmp.

2) Unpack the tarball

Next, unpack the tarball to the right place under /usr/lib/jvm.

sudo mkdir -p /usr/lib/jvm
sudo tar -x -C /usr/lib/jvm -f /tmp/jdk-8u102-linux-x64.tar.gz

Unpacking will create the directory jdk1.8.0_102 under /usr/lib/jvm.

3) Create the .jinfo file

The jinfo file is a kind of manifest of executables bundled in the JDK. Go ahead and create the file /usr/lib/jvm/.jdk1.8.0_102.jinfo with the contents below. Note the leading “.” in the filename, it is important!

4) Update Permissions

Let’s fix the permissions on all the files before moving on:

sudo chown -R root:root /usr/lib/jvm/.jdk1.8.0_102.jinfo /usr/lib/jvm/jdk1.8.0_102

5) Update Alternatives

Next we’ll use the update-alternatives command to register all the java executables. These will create symlinks for each of them. To do this, run the following commands:

6) Update Java Alternatives

Finally, use the update-java-alternatives command (which you may need to install explicitly first) to point all the java executable symlinks to the right version’s java executables.

sudo apt-get -yq install java-common
sudo update-java-alternatives -s jdk1.8.0_102

You can check if it has been registered properly:

$ update-java-alternatives -l
jdk1.8.0_102                   180        /usr/lib/jvm/jdk1.8.0_102

You’re All Set!

You should now be able to invoke any java executable:

$ java -version
java version "1.8.0_102"
Java(TM) SE Runtime Environment (build 1.8.0_102-b14)
Java HotSpot(TM) 64-Bit Server VM (build 25.102-b14, mixed mode)

If you have multiple versions installed, for example if you also have OpenJDK 8 also installed, you can switch between the two:

# both versions are listed as alternatives
$ update-java-alternatives -l
java-1.8.0-openjdk-amd64       1081       /usr/lib/jvm/java-1.8.0-openjdk-amd64
jdk1.8.0_102                   180        /usr/lib/jvm/jdk1.8.0_102

# currently using Oracle JDK 1.8
$ java -version
java version "1.8.0_102"
Java(TM) SE Runtime Environment (build 1.8.0_102-b14)
Java HotSpot(TM) 64-Bit Server VM (build 25.102-b14, mixed mode)

# swtich to OpenJDK 8 (errors are for browser plugin and can be ignored)
$ sudo update-java-alternatives -s java-1.8.0-openjdk-amd64
update-alternatives: error: no alternatives for
update-java-alternatives: plugin alternative does not exist: /usr/lib/jvm/java-8-openjdk-amd64/jre/lib/amd64/

# now using OpenJDK 8
$ java -version
openjdk version "1.8.0_91"
OpenJDK Runtime Environment (build 1.8.0_91-8u91-b14-3ubuntu1~16.04.1-b14)
OpenJDK 64-Bit Server VM (build 25.91-b14, mixed mode)