Automated Oracle Java Install on Ubuntu with default java and java alternatives

If you need or prefer to run Oracle Java instead of OpenJDK then the below tips and script may be useful to you. The below script provides an automated installation on Ubuntu. Everything for the install steps in the script are explained in detail below the script.

#!/bin/bash
# Installs Oracle JDK
# @author Erik Paulsson

# the name of the java package to install
JAVA_PKG=oracle-java7-installer

# the name of the directory created by the installer in /usr/lib/jvm
JAVA_DIR=java-7-oracle

# Add the WebUpd8 Java repo
echo 'deb http://ppa.launchpad.net/webupd8team/java/ubuntu precise main' > /etc/apt/sources.list.d/webupd8team-java.list
echo 'deb-src http://ppa.launchpad.net/webupd8team/java/ubuntu precise main' >> /etc/apt/sources.list.d/webupd8team-java.list
apt-key adv --keyserver keyserver.ubuntu.com --recv-keys EEA14886
apt-get update

# Accept the Oracle Java license in an automated fashion, otherwise the
# 'apt-get install' of the package will prompt for accepting the Oracle license
echo debconf shared/accepted-oracle-license-v1-1 select true | sudo debconf-set-selections
echo debconf shared/accepted-oracle-license-v1-1 seen true | sudo debconf-set-selections

# Now we can install java without any prompts
apt-get install $JAVA_PKG -y

# Create symlinks for default-java under /usr/lib/jvm
# /usr/lib/jvm/default-java is a standard on debian systems and
# Tomcat looks to see if it exists for setting JAVA_HOME
sudo ln -s /usr/lib/jvm/$JAVA_DIR /usr/lib/jvm/default-java
# This symlink is used by the 'update-java-alternatives' command
# in order to know which commands are located where for creating symlinks to them in /etc/alternatives
sudo ln -s /usr/lib/jvm/.$JAVA_DIR.jinfo /usr/lib/jvm/.default-java.jinfo
sudo update-java-alternatives -s default-java

WebUpd8 provides a PPA repository with packages to install the latest versions of Oracle Java 7 and 8. These packages don’t actually host the Java installer / binaries, but instead downloads the installer / binaries from the official Oracle website and installs it for you. The installation will create a directory in /usr/lib/jvm, the directory name will be determined by the version of Java installed. The standard and default location for all versions of Java on Ubuntu is in /usr/lib/jvm and your JAVA_HOME directory will be a directory located here. For example, the above script installs the package ‘oracle-java7-installer’ which creates a directory named ‘java-7-oracle’ in /usr/lib/jvm, so your JAVA_HOME would be /usr/lib/jvm/java-7-oracle. However, if you use a JAVA_HOME environment variable it is better not to have to update it when a new version of java is installed AND many applications try to set their own JAVA_HOME variable by looking for java in some standard install locations. It is standard to create a symlink at /usr/lib/jvm/default-java that points to your actual java installation directory of the version of java that you want to use. Then, if you are defining a JAVA_HOME environment variable its value should be /usr/lib/jvm/default-java.

Ubuntu uses what it calls the ‘alternatives’ system for allowing multiple versions of the same software to be installed on a machine and defining which is the default version to use. The default versions for many software installations are defined by symlinks in /etc/alternatives.
Read more about the ‘alternatives’ system here: http://manpages.ubuntu.com/manpages/lucid/man8/update-alternatives.8.html
To configure / define all java related alternatives use the ‘update-java-alternatives’ command. See: http://askubuntu.com/questions/141791/is-there-a-way-to-update-all-java-related-alternatives
The ‘update-java-alternatives’ command looks for a hidden file in ‘/usr/lib/jvm’ to get information on where all the java related commands are located in order to create the ‘/etc/alternatives’ symlinks. This hidden file’s name uses a naming convention based on the directory name of the corresponding java installation. Using the above example with a java install at ‘/usr/lib/jvm/java-7-oracle’ then the ‘update-java-alternatives’ command would look for the hidden file ‘/usr/lib/jvm/.java-7-oracle.jinfo’. But, we want to use the standard ‘/usr/lib/jvm/default-java’ as our default install for our java alternatives and we can do this easily by creating the symlink ‘/usr/lib/jvm/.default-java.jinfo’ and pointing it to ‘/usr/lib/jvm/.java-7-oracle.jinfo’ before we run ‘update-java-alternatives -s default-java’.

paulsson@snapper:/$ ls -la /usr/lib/jvm/
total 24
drwxr-xr-x  3 root root  4096 Feb 19 17:38 .
drwxr-xr-x 68 root root 12288 Feb 27 06:44 ..
lrwxrwxrwx  1 root root    26 Feb 19 17:38 default-java -> /usr/lib/jvm/java-7-oracle
lrwxrwxrwx  1 root root    33 Feb 19 17:38 .default-java.jinfo -> /usr/lib/jvm/.java-7-oracle.jinfo
drwxr-xr-x  8 root root  4096 Feb 19 17:38 java-7-oracle
-rw-r--r--  1 root root  2583 Feb 19 17:38 .java-7-oracle.jinfo

After running the ‘java-update-alternatives’ command your java related /etc/alternatives links will look something like this:

paulsson@snapper:/$ ls -la /etc/alternatives/ | grep java
lrwxrwxrwx   1 root root    43 Feb 19 17:38 appletviewer -> /usr/lib/jvm/java-7-oracle/bin/appletviewer
lrwxrwxrwx   1 root root    53 Feb 19 17:38 appletviewer.1.gz -> /usr/lib/jvm/java-7-oracle/man/man1/appletviewer.1.gz
lrwxrwxrwx   1 root root    34 Feb 19 17:38 apt -> /usr/lib/jvm/java-7-oracle/bin/apt
lrwxrwxrwx   1 root root    44 Feb 19 17:38 apt.1.gz -> /usr/lib/jvm/java-7-oracle/man/man1/apt.1.gz
....
lrwxrwxrwx   1 root root    34 Feb 19 17:38 jar -> /usr/lib/jvm/java-7-oracle/bin/jar
lrwxrwxrwx   1 root root    44 Feb 19 17:38 jar.1.gz -> /usr/lib/jvm/java-7-oracle/man/man1/jar.1.gz
lrwxrwxrwx   1 root root    40 Feb 19 17:38 jarsigner -> /usr/lib/jvm/java-7-oracle/bin/jarsigner
lrwxrwxrwx   1 root root    50 Feb 19 17:38 jarsigner.1.gz -> /usr/lib/jvm/java-7-oracle/man/man1/jarsigner.1.gz
lrwxrwxrwx   1 root root    39 Feb 19 17:38 java -> /usr/lib/jvm/java-7-oracle/jre/bin/java
lrwxrwxrwx   1 root root    45 Feb 19 17:38 java.1.gz -> /usr/lib/jvm/java-7-oracle/man/man1/java.1.gz
lrwxrwxrwx   1 root root    36 Feb 19 17:38 javac -> /usr/lib/jvm/java-7-oracle/bin/javac
lrwxrwxrwx   1 root root    46 Feb 19 17:38 javac.1.gz -> /usr/lib/jvm/java-7-oracle/man/man1/javac.1.gz
lrwxrwxrwx   1 root root    38 Feb 19 17:38 javadoc -> /usr/lib/jvm/java-7-oracle/bin/javadoc
lrwxrwxrwx   1 root root    48 Feb 19 17:38 javadoc.1.gz -> /usr/lib/jvm/java-7-oracle/man/man1/javadoc.1.gz
....

Now you can follow how your ‘java’ command gets resolved to its install location:

paulsson@snapper:/$ which java
/usr/bin/java
paulsson@snapper:/$ ls -la /usr/bin/java
lrwxrwxrwx 1 root root 22 Feb 19 17:38 /usr/bin/java -> /etc/alternatives/java
paulsson@snapper:/$ ls -la /etc/alternatives/java
lrwxrwxrwx 1 root root 39 Feb 19 17:38 /etc/alternatives/java -> /usr/lib/jvm/java-7-oracle/jre/bin/java

In the above examples ‘/usr/lib/jvm/java-7-oracle’ is the path to our current default java even though we have /usr/lib/jvm/default-java as a symlink pointing to /usr/lib/jvm/java-7-oracle because of the values defined for java related commands in /usr/lib/jvm/.default-java.jinfo which is a symlink pointing to /usr/lib/jvm/.java-7-oracle.jinfo. If you take a look at the contents of /usr/lib/jvm/.default-java.jinfo you will see how ‘update-java-alternatives’ sets up the links in /etc/alternatives/.

Let me know in the comments if you would like to see how this can be done for Red Hat based linux or if you’d like to have this same install done via Puppet.

To read more about installing Java on Ubuntu please see the below links:
https://help.ubuntu.com/community/Java
http://www.webupd8.org/2012/01/install-oracle-java-jdk-7-in-ubuntu-via.html
http://www.webupd8.org/2012/06/how-to-install-oracle-java-7-in-debian.html