How To Install And Manage Java On Ubuntu

Java becomes the best Internet development standard for enterprise Web application development. Today, many programs require to have Java installed. On this article we will try to guide you how to install and manage different versions of Java. But before we start let’s review the several implementations of Java.

java

When we talk about java three implementations are common:

  • OpenJREis the official implementation of Java Runtime Environment (JRE) for your Ubuntu systems and should suffice to run any Java program that you might require.
  • OpenJDK: The OpenJDK project is an open-source implementation of the Java SE Platform. This is the default version of Java that is provided from a supported Ubuntu repository. Currently, there are two versions available, openjdk-6 and openjdk-7.
  • Oracle Java: Oracle Java is the official JDK Java Se Platform version 7 implementation from Oracle.

Installing JRE/JDK :

Now we will give you the instructions to install the easiest option. OpenJDK 6 will be installed on Ubunto 12.04 and OpenJDK 7 will be installed on Ubunto 12.10+.

The installation of Java will be with the “apt-get” command. Firstly, we will update the package index using the following command:

sudo apt-get update

Then we will check if there is already an installation of Java using:

java -version

If the response of the execution of the previous command shows that “the program java can be found in the following packages” that is mean that you can start the installation of your tool. For that we will use the following command:

sudo apt-get install default-jre

Executing this command will enable you to install Java Runtime Environment (JRE). It is important to know that the compilation of Java applications needs to have already a JDE installed. So if you don’t have it, you can make the installation using the following command:

sudo apt-get install default-jdk

The giving instructions are the most important to install Java. The following steps are optional and will be used just when you need them.

Installing OpenJDK 7:

For the installation of OpenJDK 7, the same tool “apt-get” is used. So you can make this installation using the following command:

sudo apt-get install openjdk-7-jre

Using this command, you will have the JRE installed. To install the other tool JDK, you just have to execute the following command:

sudo apt-get install openjdk-7-jdk

Installing Oracle JDK:

The installation of this tool also may be made using the “apt-get” command. So for that you have to execute the following command:

sudo apt-get install python-software-properties
sudo add-apt-repository ppa:webupd8team/java
sudo apt-get update

There are 3 versions of JDK will be introduced in this article. We will start the installation of the first version Oracle JDK 6 is an old version but is stilling used. So for the installation you need to execute the following command:

sudo apt-get install oracle-java6-installer

The second version is Oracle JDK 7 so to make the installation the following command is used:

sudo apt-get install oracle-java7-installer

And our last version is Oracle JDK 8 which is a developer preview released on March 2014. It will be installed using the following command:

sudo apt-get install oracle-java8-installer

How to manage Java:

We supposed in this section that you have two Java installations so it is important to choose which one will be used by default.

Start by executing the following command:

sudo update-alternatives --config java

Since you have two installations, so you will get something like the following:

There are 2 choices for the alternative java (providing /usr/bin/java).
Selection     Path                                                                                Priority   
Status
------------------------------------------------------------* 
0            /usr/lib/jvm/java-7-oracle/jre/bin/java                                               1062    
auto mode  
1            /usr/lib/jvm/java-6-openjdk-amd64/jre/bin/java                                        1061     
manual mode  
2            /usr/lib/jvm/java-7-oracle/jre/bin/java                                               1062     
manual mode Press enter to keep the current choice[*], or type selection number:

So to choose between them, you will have just to choose the number as default.

The some command may be used for Java compiler:

sudo update-alternatives --config javac

You will get also the same screen and you will need just to choose the number which represents you choice between the existing installations.

How to set up Java_Home environment variable:

Some programs need to have the Java_Home environment variable, so to set this tool we will use the following instructions. Firstly, it is required to find the path of the Java installation in your system by using the following command:

sudo update-alternatives --config java

You will get something like the following scree:

There are 2 choices for the alternative java (providing /usr/bin/java). 
Selection    Path                                                                                 Priority  
Status
------------------------------------------------------------* 
0            /usr/lib/jvm/java-7-oracle/jre/bin/java                                                 1062     
auto mode 
 1         /usr/lib/jvm/java-6-openjdk-amd64/jre/bin/java                                            1061      
manual mode  
2            /usr/lib/jvm/java-7-oracle/jre/bin/java                                                 1062     
manual mode Press enter to keep the current choice[*], or type selection number:

You can remark the path of the different Java installation appear in this screen:

/usr/lib/jvm/java-7-oracle
/usr/lib/jvm/java-6-openjdk-amd64
/usr/lib/jvm/java-7-oracle

Then copy the path of your selected installation and edit the file “/etc/environment” using the following command:

sudo nano /etc/environment

To edit this file, you will just change the path by adding your path to the following command:

JAVA_HOME="YOUR_PATH"

Then reload this file to check that the path was changed using the following command:

source /etc/environment

And Finally, execute it using:

echo $JAVA_HOME

Generally, you will get the corrected path which justify that the environment variable was set successfully.