Java JRE (8) on Ubuntu Server 16.04


Java is a pain. It's needed for so much yet it is such a pig to get the right versions etc, I recently required a very specific version of java 8 (update 45) so thought I would document the procedure I followed to get it working proficiently.


The first step is to actually download the required files. This can itself be a problem, the version I am using is jdk-8u45-linux-x64.tar.gz. One of the best ways I have found to come across these files is to literally google the file name you're looking for.

The file name is made from either jdk or jre followed by the version, build, platform, architecture and finally the extension. For example if you were looking for the JRE version 7 update 60 on 64 linux you'd want jre-7u60-linux-x86.tar.gz (I'm not even sure if this exists, but you get the point).

If you're looking to just run a java program, the Java Runtime Environment (JRE) is sufficient, however if all you can find is the Java Development Kit (JDK) that is fine. The JDK contains the JRE as well as files required for building and compiling java programs.

So get the file onto your server somehow, I used wget from a local file server:

wget http://<yourserver>/jdk-8u45-linux-x64.tar.gz  
root@shawshank-db1:/home/user# wget  
--2016-09-30 12:06:26--
Resolving (  
Connecting to (||:80... connected.  
HTTP request sent, awaiting response... 200 OK  
Length: 173271626 (165M) [application/octet-stream]  
Saving to: ‘jdk-8u45-linux-x64.tar.gz’

jdk-8u45-linux-x64.tar.g 100%[==================================>] 165.24M   646MB/s    in 0.3s    

2016-09-30 12:06:26 (646 MB/s) - ‘jdk-8u45-linux-x64.tar.gz’ saved [173271626/173271626]


Now we've got the right files we need to extract the archive:

tar -xzvf jdk-8u45-linux-x64.tar.gz  
root@shawshank-db1:/home/user# tar -xzvf jdk-8u45-linux-x64.tar.gz  
<output omitted>  

This has created a directory with all the relevant files.

Now we need to put this somewhere, I like using /usr/lib/jvm/ as my java directory, but it really doesn't matter. We can then move our extracted files to that directory.

mkdir /usr/lib/jvm  
mv jdk1.8.0_45/ /usr/lib/jvm/  

The steps I followed are slightly different, but you get the picture:

root@shawshank-db1:/home/user# cd /usr/lib/  
root@shawshank-db1:/usr/lib# mkdir /usr/lib/jvm  
root@shawshank-db1:/usr/lib# mv /home/user/jdk1.8.0_45/ jvm/  
root@shawshank-db1:/usr/lib# cd jvm  
root@shawshank-db1:/usr/lib/jvm# ls  

Now we need to actually tell linux where the java executable is. Personally I like to have the flexibility to install multiple options. So i first create a 'default-java' symlink which I can change in the future to move between versions

ln -s /usr/lib/jvm/jdk1.8.0_45 /usr/lib/jvm/default-java  
root@shawshank-db1:/usr/lib/jvm# ln -s /usr/lib/jvm/jdk1.8.0_45 /usr/lib/jvm/default-java  
root@shawshank-db1:/usr/lib/jvm# ls  
default-java  jdk1.8.0_45  

Now the java path itself is inside the jre/bin directories within default-java. This may vary depending on your installation package.

root@shawshank-db1:/usr/lib/jvm# ls default-java/jre/bin  
ControlPanel  java  javaws  jcontrol  jjs  keytool  orbd  pack200  policytool  rmid  rmiregistry  servertool  tnameserv  unpack200  

Let's add a link to java from the system path.

ln -s /usr/lib/jvm/default-java/jre/bin/java /usr/bin/java  
root@shawshank-db1:/usr/lib/jvm# ln -s /usr/lib/jvm/default-java/jre/bin/java /usr/bin/java  
root@shawshank-db1:/usr/lib/jvm# java -version  
java version "1.8.0_45"  
Java(TM) SE Runtime Environment (build 1.8.0_45-b14)  
Java HotSpot(TM) 64-Bit Server VM (build 25.45-b02, mixed mode)  

The program I am using tries to look for a JAVA_HOME bash environment variable. We can set that using the export keyword but that will only last for the current session, to make it persistent we need to add it to the .bashrc file in the user's directory.

echo 'export JAVA_HOME="/usr/lib/jvm/default-java/jre"' >> /root/.bashrc  
root@shawshank-db1:/usr/lib/jvm# echo 'export JAVA_HOME="/usr/lib/jvm/default-java/jre"' >> /root/.bashrc  
root@shawshank-db1:/usr/lib/jvm# cat /root/.bashrc | tail  
# this, if it's already enabled in /etc/bash.bashrc and /etc/profile
# sources /etc/bash.bashrc).
#if [ -f /etc/bash_completion ] && ! shopt -oq posix; then
#    . /etc/bash_completion

export JAVA_HOME="/usr/lib/jvm/default-java/jre"  


wget http://<yourserver>/jdk-8u45-linux-x64.tar.gz  
tar -xzvf jdk-8u45-linux-x64.tar.gz  
mkdir /usr/lib/jvm  
mv jdk1.8.0_45/ /usr/lib/jvm/  
ln -s /usr/lib/jvm/jdk1.8.0_45 /usr/lib/jvm/default-java  
ln -s /usr/lib/jvm/default-java/jre/bin/java /usr/bin/java  
echo 'export JAVA_HOME="/usr/lib/jvm/default-java/jre"' >> /root/.bashrc  
java -version