Search This Blog

Thursday, July 9, 2015

Install Sun/ Oracle JDK on Ubuntu

For certain programs, I needed to install Sun/ Oracle JDK on my machine. My default JDKs were different versions of IcedTea that would not suffice for what I needed to do (To setup IcedTea check my previous post). In this post, we would see how to install Sun/ Oracle JDK on Ubuntu

My way of checking it was to list contents links to java through the following command..

> sudo update-alternatives --list java


I also verified the contents of the /usr/lib/jvm folder to make sure I had not missed configuring a previously installed JDK.

> ls /usr/lib/jvm


As you can see, I don't see a Sun JDK installed, and I need to start from the beginning.

First step was to go to Oracle site for downloading the latest JDK (http://www.oracle.com/technetwork/java/javase/overview/index.html). Follow the links to the latest version and download it to your local machine.


Next, I copied the downloaded JDK installation file from my Downloads folder to my local folder ~/Work/Servers/jdk , where I created the following script in a shell file called install-jdk1.8.sh.

#!/bin/sh

tar -xvf jdk-8*
sudo mkdir /usr/lib/jvm
sudo mv ./jdk1.8* /usr/lib/jvm/jdk1.8.0-oracle
sudo update-alternatives --install "/usr/bin/java" "java" "/usr/lib/jvm/jdk1.8.0-oracle/bin/java" 1
sudo update-alternatives --install "/usr/bin/javac" "javac" "/usr/lib/jvm/jdk1.8.0-oracle/bin/javac" 1
sudo update-alternatives --install "/usr/bin/javaws" "javaws" "/usr/lib/jvm/jdk1.8.0-oracle/bin/javaws" 1
sudo chmod a+x /usr/bin/java
sudo chmod a+x /usr/bin/javac
sudo chmod a+x /usr/bin/javaws

Here is a syntax coloured screenshot of the script.


Next we make this script executable, and execute it.

> chmod a+x *.sh
>./install-jdk1.8.sh


Once the script has run, we can test the java version to confirm everything got setup correctly..

> java -version


If you see everything setup correctly, its great. If you still see an older version of JDK, use the update-alternatives command to set it up correctly by following these steps. Also, many softwares require an explicit JAVA_HOME to be set to execute correctly. This post on setting JAVA_HOME, shows how I have set it up on my machine.


No comments: