Java JRE (8) on Ubuntu Server 16.04

Problem:

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.

Solution:

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 http://svs-ftp.example.com/Java/linux/jdk-8u45-linux-x64.tar.gz  
--2016-09-30 12:06:26--  http://svs-ftp.example.com/Java/linux/jdk-8u45-linux-x64.tar.gz
Resolving svs-ftp.example.com (svs-ftp.example.com)... 10.53.230.126  
Connecting to svs-ftp.example.com (svs-ftp.example.com)|10.53.230.126|: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]

root@shawshank-db1:/home/user#  

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  
jdk1.8.0_45/  
jdk1.8.0_45/jre/  
jdk1.8.0_45/jre/THIRDPARTYLICENSEREADME.txt  
jdk1.8.0_45/jre/plugin/  
jdk1.8.0_45/jre/plugin/desktop/  
jdk1.8.0_45/jre/plugin/desktop/sun_java.png  
jdk1.8.0_45/jre/plugin/desktop/sun_java.desktop  
<output omitted>  
root@shawshank-db1:/home/user#  

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  
jdk1.8.0_45  
root@shawshank-db1:/usr/lib/jvm#  

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  
root@shawshank-db1:/usr/lib/jvm#  

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  
root@shawshank-db1:/usr/lib/jvm#  

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)  
root@shawshank-db1:/usr/lib/jvm#  
(Optional)

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
#fi

export JAVA_HOME="/usr/lib/jvm/default-java/jre"  
root@shawshank-db1:/usr/lib/jvm#  

Summary

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