Friday, December 26, 2014

Installing Oracle Java JDK8 On Ubuntu 14.04

So you have installed a brand-new Ubuntu 14.04 LTS OS on your PC and tweaked it just the way you like it. Now you want to do a bit of Java programming on it and wonder how to go about doing it. Let this article be the guide to show you how to install Oracle's JDK 8 Java and set it up properly.

First you should be aware that there is no Java installed on a freshly intalled Ubuntu 14.04 LTS. This can be verified easily by executing 'java -version' in a terminal. This article assumes that there is no Java installed.

Why Oracle Java? Why not OpenJDK? Good question and the answer is that it is recommended by Google. Should you plan to use Java for Android programming later, then Google says it is better to use Oracle's version - their Android Studio says that there are stability issues with OpenJDK. Whether this is true is a moot point here - if you do plan to do Android programming later, then install Oracle's Java - else, any Java will do, even OpenJDK.

Here are the steps for installing Oracle JDK 8. Note that a web browser is used to download the JDK - not Ubuntu Software Center or Synaptic.

  1. Point your browser to here and click on the JDK download button (not the Server JRE or JRE). On the page that appears next, click to 'Accept License Agreement' and select either the 32-bit or 64-bit tar.gz file to start the download. Save the file and wait for the download to finish. If you have not changed anything, the JDK file will be found in your Home folder Downloads location.
  2. On the same web page, scroll down and click on either the 32-bit or 64-bit Demo and Sample file for downloading. Again, save the file and wait for the download to finish,
  3. Use the 'Back' key for your browser to go back one page and scroll down until you see Java SE 8 Documentation and click on the download button. On the next page, accept the license agreement and click on download the zip file for the documentation for the JDK (not the JavaFX). Save the file and wait for the download to finish.
Now that you have downloaded the files, it's time to install it. First open a terminal and change directory to the Downloads folder (or to the folder in which you saved the downloaded JDK files) by executing:

  cd Downloads

Then extract the contents of the 'jdk-8u25-linux-x64.tar.gz' file by executing:

  tar xzvf jdk-8u25-linux-x64.tar.gz

This will create a  'jdk1.8.0_25' directory in the Downloads directory while displaying a lot of stuff on the terminal's screen. After this is completed and you are back to the command prompt, you can extract the demos and samples by executing:

  tar xzvf jdk-8u25-linux-x64-demos.tar.gz

This will create 2 directories under the 'jdk1.8.0_25' directory - 'sample' and 'demo' directories. When completed, you can now extract the Java API documents by executing:

  unzip -d jdk1.8.0_25

This will create a 'doc' directory under the 'jdk1.8.0_25' directory. After this is completed, you now have all the files necessary for the Oracle Java JDK 8 - all you have to do is to put them in the proper place.

The 'proper place' can be anywhere - your home directory (if you want Java for your own use only) or '/usr/local/' or 'usr/lib/jvm', etc. Note that if you want Java to reside as a sub-directory in '/usr/local/' or '/usr/lib/jvm/', then you need to use 'sudo' since administrator privileges are needed to create sub-directories there. This is not required if you install Java in your home directory. For this article, we will use '/usr/lib/jvm/' directory as this is where all Java related files end up if it was installed by default.

First we have to create the /usr/lib/jvm directory by executing:

  sudo mkdir /usr/lib/jvm

Next, we move the 'jdk1.8.0_25' directory to /usr/lib/jvm

  sudo mv ~/Downloads/jdk1.8.0_25/ /usr/lib/jvm/

The directory will be moved from your home Downloads directory to the '/usr/lib/jvm/' directory. You can verify this by listing the contents of the 'usr/lib/jvm/' directory using the 'ls -la' command. Note that the owner and group for the jdk1.8.0_25 directory is yours but you can easily change this by executing:

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

Again, you can verify this by using the 'ls -la' command.

The next thing to do is to tell Ubuntu where Oracle Java JDK resides. For this we use the 'update-alternatives' command. Execute the following commands one by one from the terminal (note that all commands are on a single line):

  sudo update-alternatives --install "/usr/bin/java" "java" "/usr/lib/jvm/jdk1.8.0_25/bin/java" 0

  sudo update-alternatives --install "/usr/bin/javac" "javac" "/usr/lib/jvm/jdk1.8.0_25/bin/javac" 0

These will install all the necessary links to Oracle's Java. Next we have to tell Ubuntu the default Java to use. Execute the following commands one by one from the terminal (note that all commands are on a single line):

  sudo update-alternatives --set java /usr/lib/jvm/jdk1.8.0_25/bin/java

  sudo update-alternatives --set javac /usr/lib/jvm/jdk1.8.0_25/bin/javac

To verify that all have been set correctly, you can use the following commands:

  update-alternatives --list java

  update-alternatives --list javac

If the output to the 2 queries above are /usr/lib/jvm/jdk1.8.0_25/bin/java and  /usr/lib/jvm/jdk1.8.0_25/bin/javac, then everything have been set correctly. Now that you have informed Ubuntu about Oracle's Java, you still have to perform one last task before Java is set up and ready for use. This is to set the environment variables and paths correctly for the whole system. Execute the following command:

  sudo nano /etc/profile

This will execute the nano text editor and open the /etc/profile file for editing. Now add the following lines at the end of the file.

  export JAVA_HOME=/usr/lib/jvm/jdk1.8.0_25
  export PATH=$PATH:$JAVA_HOME/bin

Save the file and close it. Note that, upon system startup, these will set the JAVA_HOME and PATH environment variables for the whole system. The JAVA_HOME environment variable points to the JDK root while the PATH environment variable will add the /bin directory of the JDK to whatever path that has already been specified. This will enable the 'javac' command to be executed from any directory.

Now log out and then log in again to the Ubuntu system in order to make sure that both the environment variables is set. Check by opening a terminal and executing:

  echo $JAVA_HOME

  echo $PATH

Then as the last check, execute the following:

  java -version

  javac -version

If you see 'java version "1.8.0_25"' and 'javac 1.8.0_25' in the output, then you are now ready to use Oracle's Java on your Ubuntu system. You can also delete the tar.gz and zip files which were downloaded from the web in order to free up disk space.

Where to go from here? If you are familiar with Java programming, you can go ahead to use Java anyway you like it. If you are a novice and this is the first time you are using Java, then I would recommend searching the web for tutorials and the books "Java For Dummies, 6th Edition" and "Head First Java, 2nd Edition" as a guide. Happy Computing!


  1. Worked perfectly on Linux Mint 17. :)

    Thanks !

  2. hii
    i followed the above procedure but got error while checking version.
    i m using ubuntu 14.04,

    plz help

    java: error while loading shared libraries: cannot open shared object file: No such file or directory

    javac: error while loading shared libraries: cannot open shared object file: No such file or directory

    Thank you

    1. See for info. Hope this help.

    2. thanks for replying... problem already sorted.

  3. Thanks for this guide! Worked perfectly :-)

  4. simply best.. really really helpful.. thank you very much.. :)

  5. You may need to run source /etc/profile to reload system wide PATH /etc/profile