Installing Java On Ubuntu

0
684
Java on Ubuntu

How to quickly install Java on your Ubuntu distribution on a per user and system wide basis.

Installing Java in Five Minutes

Good lord. All I want to do is install the Java SDK and Java Runtime on my Ubuntu system. I’ve pretty much went around the web and read so many ways to do this that it makes my head spin. In every case, it was too much information, too much work.

What I am going to deliver here is a dead simple way to install Java on your system. It should take you less than 5 mere minutes to read through this and get it running quickly.

With that out of the way, there are two ways to install Java:

  1. Per User
  2. System Wide

Lets look at how to install per each user first.

Per User Java Installation

Type the following in and see if Java is already installed. If you just installed a new Ubuntu desktop distribution, it probably won’t be:

java -version

On return you may get a message like this:

The program 'java' can be found in the following packages:
 * default-jre
 * gcj-4.8-jre-headless
 * openjdk-7-jre-headless
 * gcj-4.6-jre-headless
 * openjdk-6-jre-headless
Try: sudo apt-get install <selected package>

Oracle Java is no longer shipped with Ubuntu. Apparently its been this way since 2011. Because of this we have only two options:

  1. Install the open source version, OpenJDK
  2. Install Oracle Java from their website

Almost every thread I read tells you how to install OpenJDK.

Don’t use OpenJDK!

Why? Because nine times out of ten, it will be an older version than Oracle’s official release. You don’t want to be running old crap do you? Of course not. The Ubuntu repositories aren’t going to have the most current Java version. It will be OpenJDK. The lure of this option is quick installation with apt-get. But don’t go this route.

Another lazy persons way to get the current Java version is by downloading a Personal Package Archives (PPA) from another entity outside of the Ubuntu repositories. I’m not too fond of that option. If you are a developer or QA engineer, don’t go this route either. You want the 100% real deal with no questions asked.

Play it safe and use the original source instead. Its only a few extra steps.

Getting Oracle Java

Go to the Oracle Java Standard Edition website. Download the Java Development Kit (JDK) which also includes the Java Runtime Environment (JRE).

In my particular case, I downloaded the Linux x64 distribution (jdk-8u31-linux-x64.tar.gz). Although the x64 signature suggests only Intel 64-bit CPUs, the download also contains support for AMD 64 bit as well.

If you are the only user on your Ubuntu desktop, put this download into your home directory under the folder named Java:

cd /home/kkobashi
mkdir java
cd java
mv Downloads/jdk-8u31-linux-x64.targ.gz .
tar -xvf jdk-8u31-linux-x64.targ.gz

After extraction, your layout should look something like this:

/home
  /kkobashi
    /java
      /jdk1.8.0_31
        javafx-src.zip
        src.zip
        /bin
        /db
        /include
        /jre
        /lib
        /man

Notice that the JRE is located inside the JDK folder. Also that the JavaFX and source are still zipped up.

Now we need to set some environment variable paths to point to the Java home directory and its binary files. An important thing to remember for later is that if you upgrade Java, don’t forget to change JAVA_HOME to point to the correct version folder.

Sudo open up your $HOME/.bashrc file with your text editor. Type this into the end of it and save:

export JAVA_HOME=$HOME/java/jdk1.8.0_31
export PATH=$PATH:$JAVA_HOME/bin

Remember, this is per user.

Reboot Ubuntu. Then, run the following to see that Java is installed:

java -version

Outputs:

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)

System Wide Java Installation

Now, if you want to force all users to use a particular Java version, then setup a system wide installation. Do this by downloading and installing Java into a system folder, say /java and then extracting it.

cd /
mkdir java
cd java
mv Downloads/jdk-8u31-linux-x64.targ.gz .
tar -xvf jdk-8u31-linux-x64.targ.gz

After extraction, your layout should look something like this:

/java
    /jdk1.8.0_31
      javafx-src.zip
      src.zip
      /bin
      /db
      /include
      /jre
      /lib
      /man

There are two ways you can setup system wide environment variables:

  1. /etc/environment
  2. /etc/profile.d/env.sh

Setting Environment Variables With /etc/environment

Open up /etc/environment. Everything in there should be a key=value line. A disadvantage to this approach is that you cannot have variable expansion.

JAVA_HOME=/java/jdk1.8.0_31
PATH="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/java/jdk1.8.0_31/bin"

Setting Environment Variables With /etc/profile.d/env.sh

If you really want variable expansion, create a /etc/profile.d/env.sh file instead (do not monkey directly with /etc/profile):

export JAVA_VERSION=1.8.0_31
export JAVA_HOME=/java/jdk$JAVA_VERSION
export PATH=$PATH:$JAVA_HOME/bin

Now that wasn’t so bad, was it?

Of Interest