Contents
Need an expert to help you with
" Installing Java on Ubuntu with apt-get" ?

Get Help

Installing Java on Ubuntu with apt-get

Java Installation on Ubuntu 14.04
Java setup on Ubuntu 14.04

Introduction to Java

Many articles and programs will require you to have Java Installed. In this tutorial we will teach you how to install and manage different versions for Java.

Installing default JRE/JDK

This is the most recommended option due to its ease; it will install OpenJDK on Ubuntu 12.04 and earlier. Note that anything above 12.10 will install OpenJDK 7. Getting Java installed with apt-get is easy. Begin with updating the package index using the below command.
sudo apt-get update
After that, check if Java is not already installed.
java -version
If case it returns ‘The program java can be found in the following packages’, it will mean that Java did not yet install. Use the command in the following.
sudo apt-get install default-jre
The command will install Java Runtime Environment (JRE). If you wanted the Java Development Kit (JDK) which is typically used to compile Java applications like Apache Ant, Apache Maven, Eclipse, and IntelliJ IDEA, then use the following command.
sudo apt-get install default-jdk
This is all you need to install Java. The next few steps are optional and should only be executed If needed.

Getting OpenJDK 7 Installed.

Install OpenJDK 7 using the following command.
sudo apt-get install openjdk-7-jre
That should install the Java Runtime Environment. If you need the Java Development Kit then use the command in the following.
sudo apt-get install openjdk-7-jdk

Installing Oracle JDK

The Oracle JDK is the official JDK. However, it is not provided by Oracle any longer as a default Installation for Ubuntu. You can still install it with apt-get. To install any version, use the following commands.
sudo apt-get install python-software-properties
sudo add-apt-repository ppa:webupd8team/java
sudo apt-get update
If you would like to install a specific version of Oracle JDK, use one of the following commands. Oracle JDK 6 This is an old version, but it is still used.
sudo apt-get install oracle-java6-installer
Oracle JDK 7 JDK 7 is the latest stable version.
sudo apt-get install oracle-java7-installer
Oracle JDK 8 This is a developer preview version.
sudo apt-get install oracle-java8-installer

Managing your Java

If you have multiple Java Installations on your Droplet, you can choose which Java version to use as default. In order to do this, use the command below.
sudo update-alternatives --config java
It should return something like that which is shown below If you had 2 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 may choose which number you want to use as default. This could also be made for the Java compiler (javac).
sudo update-alternatives --config javac
This should be the same selection screen like shown in the previous command and needs to be used in the same way. This command may be executed for any other commands which can possibly have different installations. In Jav this includes, but is not restricted to, keytool, javadoc and jarsigner.

Setting the “JAVA_HOME” environment variable

To select the ‘JAVA_HOME’ environment variable, which is required for a couple of programs, use the following command in order to find out what the path of your Java installation is.
sudo update-alternatives --config java
It will return something like the following. There should be 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 path of the installation is for each. /usr/lib/jvm/java-7-oracle /usr/lib/jvm/java-6-openjdk-amd64 /usr/lib/jvm/java-7-oracle Copy the path from your preferred installation and then edit the file ‘/etc/environment’.
sudo nano /etc/environment
In this file, add the following line replacing ‘YOUR_PATH’ with the just copied path.
JAVA_HOME="YOUR_PATH"
This will be enough to select the environment variable. Now reload the file.
source /etc/environment
Test it by executing the below.
echo $JAVA_HOME
In case it returns the just set path, the environment variable has been set successfully. If it does not do this, please make sure you followed all steps correctly.

Conclusion

Now you should have Java Installed on your Ubuntu system.
Updated on 03 Oct 2017
Tagged:
0 Comments
Add a comment
Login for Comment