Categories
Linux

How to install Java Runtime Environment on Ubuntu in 2020

Java is tricky these days. Last year, Oracle changed its license and useful resources like the “WebUpd8” Team PPA went offline. Oracle does provide downloads on its website, but there’s really no obvious way to install and use them if you just want to run some Java programs that OpenJDK for some reason can’t deal with. Fortunately, it’s very easy.

Before we start, you really do need to make sure whatever you’re trying to run won’t work with OpenJDK. The current official Ubuntu OpenJDK version is 11, but I can also see version 8 in my repos (please don’t ask me what the difference between them is, I have no idea) so install openjdk-11-jre or openjdk-8-jre with Synaptic or apt-get and see if that works. If it does, you’re done. If not, read on.

To get the official Oracle flavour of Java, go to java.com, press the big red Java Download button and download one of the non-RPM packages listed there, like Linux_x64. Currently, the downloaded file is called jre-8u251-linux-x64.tar.gz and I saved it in ~/Downloads.

OpenJDK (and also previously the WebUpd8 version) installs in /usr/lib/jvm and that’s what I’ll be using to install what I just got.

Open the terminal, go to /usr/lib/jvm and move the file you downloaded there:

cd /usr/lib/jvm
sudo mv ~/Downloads/jre-8u251-linux-x64.tar.gz /usr/lib/jvm

Unpack it:

sudo tar zxvf jre-8u251-linux-x64.tar.gz

You can and should now delete the file you downloaded:

sudo rm jre-8u251-linux-x64.tar.gz

and find out what directory was created when you unpacked the file:

ls

Which, among possible other results, will give you what you need:

jre1.8.0_251

Technically, Java is now installed, but to run it you’d need to constantly use the full installation path, which is not very practical. We need to make sure the system knows about it and that’s where update-alternatives comes in.

If you have another Java version already installed from package and you try to run java, it’ll be the one to run, and not the one you just got:

java -version

for me results in:

openjdk version "11.0.7" 2020-04-14
OpenJDK Runtime Environment (build 11.0.7+10-post-Ubuntu-3ubuntu1)
OpenJDK 64-Bit Server VM (build 11.0.7+10-post-Ubuntu-3ubuntu1, mixed mode, sharing)

which is not really what I had in mind.

update-alternatives lets you manually add versions of programs to your system to switch between. It’s done like this:

update-alternatives --install systemwide-program-executable program-name actual-program-executable priority

Which means you need to do something like this:

sudo update-alternatives --install /usr/bin/java java /usr/lib/jvm/jre1.8.0_251/bin/java 1207

An explanation:

/usr/bin/java: The system will look for programs like java in /usr/bin, so that’s where it needs to be in.

java: That’s the name update-alternatives will use for the group of programs to choose between.

/usr/lib/jvm/jre1.8.0_251/bin/java: that’s the path to the java executable that was just unpacked. It’s located in bin inside the directory that got created when you unpacked the downloaded file, so the full path looks like that.

1207: the priority is less than important right now, OpenJDK had priority 1111 so I put in 1207. You can read more about priority in the manual for update-alternatives.

Now that update-alternatives knows of our newly-installed Java version you need to set it as the default:

sudo update-alternatives --config java

and press the number corresponding to the Java you want to use.

That’s it. You can check that your newly installed Java is the one the system will run by default:

java -version

Should give you:

java version "1.8.0_251"
Java(TM) SE Runtime Environment (build 1.8.0_251-b08)
Java HotSpot(TM) 64-Bit Server VM (build 25.251-b08, mixed mode)

Success!

Acknowledgments:

Info on how to add something to update-alternatives taken from this thread: https://www.linuxquestions.org/questions/linux-software-2/how-to-add-a-program-to-update-alternatives-631303/

Leave a Reply

Your email address will not be published. Required fields are marked *