How to install JAVA on Ubuntu, using the Apt-Get command.

Several programs require a JDK or JRE to be running on server , this article is a step by step guide on installing JAVA. .

Default installation

This guide shows how to install OpenJDK 6 on Ubuntu 12.04 and earlier and Open JDK 7 on ubuntu 12.10+.
Start by updating the apt-get package index

 sudo apt-get update   

Then Confirm that java is not already installed

 java -version  

If the output is : The program java can be found in the following packages , it means java has not been installed then execute the following command.

 sudo apt-get install default-jre 

This will get the JAVA run time environment up and running, for the Java Development Kit (JDK) Which is used in compiling java applications written with Ant, Maven,Eclipse or intelliJ IDEA fire up the following code

 sudo apt-get install default-jdk

That’s all for a basic JAVA installation.

Installing OpenJDK 7

To get started with the run time environment fire up the following command

 sudo apt-get install openjdk-7-jre

For the Java development kit (JDK) execute

 sudo apt-get install openjdk-7-jdk

Installing Oracle JDK

This is the official JDK provided by oracle but is no longer supported by oracle, To start first fire up the following command

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

Then depending on the version of JDK you need

Oracle JDK 6

 sudo apt-get install oracle-java6-installer

Oracle JDK 7 Stable version

 sudo apt-get install oracle-java7-installer

Oracle JDK 8

This was released in March 2014

 sudo apt-get install oracle-java8-installer

Managing Your JAVA installations , when you have multiple JAVA installations on your server , setting one as default, Can be done by firing up the following command

 sudo update-alternatives --config java 

This would be the output if your server has two installations:

 
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 then choose a number to toggle your default run time environment

To change your default compiler (javac):

 sudo update-alternatives --config javac

You can then choose a number to toggle your default java compiler . In the same way the config command can be used for other commands in JAVA which might have Multiple installations. Such as : keytool javadoc and jarsigner.

Setting up environmental variables such as “JAVA_HOME” You start by finding out the path of your Java installation by firing up

 sudo update-alternatives --config java

The output is :

 
  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.

The installation paths for each will be seen as :

 1. /usr/lib/jvm/java-7-oracle
 2. /usr/lib/jvm/java-6-openjdk-amd64
 3. /usr/lib/jvm/java-7-oracle

Copy the path from your choice installation then open up the file /etc/environment with you favorite text editor

 sudo vi /etc/environment

Then replace YOUR_PATH in the environment file with the one just copied.

 JAVA_HOME=”YOUR_PATH”

The environment variables are now set, now reload the file

 source /etc/environment

Test by firing up:

 echo $JAVA_HOME 

If the path just setup is printed out, then the environmental variables where set up successfully. If it didn’t review all the steps to be sure you did them correctly.