Monday, February 2, 2015

Installing Oracle’s JDK on Ubuntu

Automated Installation


These comamnds will install Oracle's JDK 8 (and the JRE 8 browser plugin):
sudo apt-add-repository ppa:webupd8team/java
sudo apt-get update
sudo apt-get install oracle-java8-installer

Note that if apt-add-repository gives you a "command not found" error you can add this command via
sudo apt-get install software-properties-common python-software-properties

The remainder of this article is dedicated to the manual download and installation of the JDK. For most users, the automated method shown above should be sufficient. As demonstrated below, verify the installation by typing "java -version" on the terminal session.


Manual Installation

  • Removing other versions
  • Downloading Oracle's JDK
  • Where to install?
  • Updating the PATH
  • Troubleshooting


Removing Other Versions


Ubuntu usually comes installed with OpenJDK. On new installations of Ubuntu, I prefer to uninstall this.

To verify if you have OpenJDK installed, type this on the command line:
java -version

OpenJDK can be removed using this command:
sudo apt-get purge openjdk*
sudo apt-get autoremove


Where do I download the JDK?


I start at Oracle’s site here:
http://www.oracle.com/technetwork/java/javase/downloads/index.html
And for this article, I’ve selected the latest JDK (1.0.8_31).




 

Where should I install the JDK?

If you are not familiar with the Linux directory layout, this article is recommended:
http://www.nixtutor.com/linux/understanding-the-linux-directory-layout/

The JDK should be installed in the “usr” directory.  This directory houses all the binaries, documentation, libraries, and header files for all the user applications. Most user binaries will be installed into this folder making this one of the largest folders in the Linux directory.

My preferred path is:
/usr/lib/jvm/jdk/1.0.8_31/
This leaves opportunty to distinguish between JDK and JRE installations, and multiple versions of each.


 

How do I install the JDK?


Typically, the file will be downloaded to your “downloads” folder in your home directory.

Using a terminal, following these steps:

1. Create the Directory Path


sudo mkdir -p /usr/lib/jvm/jdk/
This creates the directory path for the new installation. The -p flag creates the directory hierarchy. You will need to run this command as the super user (su + do = sudo).

2. Move the File


sudo mv ~/Downloads/jdk-8u31-linux-x64.tar.gz /usr/lib/jvm/jdk/
Move the downloaded file to the installation path.

3. Unzip the File


cd /usr/lib/jvm/jdk/
sudo tar -zxvf jdk-8u31-linux-x64.tar.gz
Navigate to the newly created folder, and unpack the downloaded file.



Setting the Path


I recommend setting the path in the /etc/environment file.

This file is specifically meant for system-wide environment variable settings. It is not a script file, but rather consists of assignment expressions, one per line.
sudo gedit /etc/environment

I've added the italicized text:
PATH="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/usr/lib/jvm/jdk/1.8.0_31/bin/
export PATH 

export JAVA_HOME=/usr/lib/jvm/jdk/1.8.0_31

To activate these path variables for the current terminal session, type
source /etc/environment

If your path variables were set successfully, you should be able to type
java –version

...and get back something that looks like this:
craigtrim@CVB:~$ java -version
java version "1.8.0_31"
Java(TM) SE Runtime Environment (build 1.8.0_31-b13)
Java HotSpot(TM) 64-Bit Server VM (build 25.31-b07, mixed mode)



Permanently Setting the Path


To permanently modify the path, modify the .bashrc file.

.bashrc is a shell script that Bash runs whenever it is started interactively. You can put any command in that file that you could type at the command prompt. You put commands here to set up the shell for use in your particular environment, or to customize things to your preferences.

Follow this command sequence:
gedit ~/.bashrc

when the text editor comes up, add this line (anywhere):
source /etc/environment
“sudo” privileges are not necessary, since this file is modified on a per-user basis.

To test your changes, exit your current command shell, and type
java -version
as you did in the prior example.


Troubleshooting



  1. If you make an error when editing the .bashrc file you can send Ubuntu into an infinite logon loop the next time you reboot. 
    1. This article contains more information.
  2. “No such file or directory” when invoking java
    1. [StackOverflow] Solution 1Solution 2
    2. Summary: 32-bit libraries are missing
      I was able to solve this using the StackOverflow link by running:
      apt-get install libc6-i386




Reference

  1. [Github] Installation Script (based on this article)
  2. [Blog] Installing Eclipse on Ubuntu 14.04
  3. [StackExchange] The purpose of .bashrc
  4. [AskUbuntu] Ubuntu gets stuck in an Infinite Loop
  5. [Help.Ubuntu] Environment Variables in Ubuntu
  6. [AskUbuntu] Using a PPA (Personal Package Archive) to install the Oracle JDK
    1. I've tested this on Ubuntu 14.10 and it works fine (4:43 PM 1/28/2015)
  7. [AskUbuntu] Uninstalling OpenJDK
  8. Installing Missing apt-add-repository Command
  9. [Github] Vagrant Recipe

3 comments:

  1. Hi, Great.. Tutorial is just awesome..It is really helpful for a newbie like me.. I am a regular follower of your blog. Really very informative post you shared here. Kindly keep blogging. If anyone wants to become a Java developer learn from Java Training in Chennai..

    ReplyDelete
  2. Excellent information with unique content and it is very useful Blog.
    DevOps Training
    DevOps Online Training

    ReplyDelete