Thursday, 6 June 2013

Install and Configure Oracle Java in Linux

Install and Configure Oracle Java in Linux

The following tutorial guides you through simple steps of  installing and configuring Oracle Java 7 on your Linux machine. These steps can be replicated on major Linux platforms such as Red Hat Enterprise Linux, CentOS, Fedora etc.

Installing Oracle Java 7 is fairly simple and easy.

The first thing to do is download the correct version and type of Java installer for your Linux machine. In my case, I am installing Java on a 64 Bit CentOS machine, hence downloading the Linux x64 version of Java.

You can download your Oracle Java from HERE

Now in my case, I already had Java installed on my machine, i.e. OpenJDK. OpenJDK (Open Java Development Kit) is a free and open source implementation of the Java Platform, Standard Edition (Java SE). You can verify whether OpenJDK is installed on your machine or not by running few simple commands listed below:
# java -version

You can even run the following command to check whether OpenJDK is installed or not:

# rpm -qa | grep -E '^open[jre|jdk]|j[re|dk]' 

You can remove Java by using the yum command:

# yum remove java

Next, execute the Oracle Java executable file that we downloaded earlier:

# rpm -ivh jdk-7u21-linux-x64.rpm

NOTE: You may need to provide execute permissions (775) for your Java executable. You can achieve this by running "chmod 775 <your JDK installer>" command.

Once the Java JDK package is installed I then needed to configure it on the system using the `alternatives` command. 

This is in order to tell the system what are the  default commands for JAVA. Most System Administrators aren’t aware about this and I think that it is a vital part when setting the JAVA package. 

# alternatives --install /usr/bin/java java /usr/java/jdk1.7.0_21/jre/bin/java 20000

# alternatives --install /usr/bin/jar jar /usr/java/jdk1.7.0_21/bin/jar 20000

# alternatives --install /usr/bin/javac javac /usr/java/jdk1.7.0_21/bin/javac 20000

# alternatives --install /usr/bin/javaws javaws /usr/java/jdk1.7.0_21/jre/bin/javaws 20000

NOTE: Make sure to change the Java version in the following commands as per your Java release.

This sets the default commands for Java JDK7 and listing the ‘/etc/alternatives/’ directory showed the following:

# ls -la /etc/alternatives/ | grep 'ja'

A final and good practice to do is to set the JAVA_HOME and PATH variable in your .bash_profile file as shown below:

Simply add the following lines to make your bash_profile file look as the one shown in the image below:

NOTE: Replace the Java version with the one that you are installing. The Path to the Java installation will remain the same.


Execute the bash_profile file and your Java is now ready for use.

# . .bash_profile

# java -version

That's it for now.. hope this tutorial helps you out.. Feel free to drop your comments/ feedback by. 


ari kesavan said...

It’s great to come across a blog every once in a while that isn’t the same out of date rehashed material. Fantastic read.
I’ve bookmarked your site, and I’m adding your RSS feeds to my Google account.
java training in bangalore

mary Brown said...

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. or learn thru Java Online Training in India . Nowadays Java has tons of job opportunities on various vertical industry.

Post a Comment