Tutorial

How To Install Java on Ubuntu 12.04 with Apt-Get

Published on February 14, 2014
English
How To Install Java on Ubuntu 12.04 with Apt-Get

Status: Deprecated

This article covers a version of Ubuntu that is no longer supported. If you are currently operating a server running Ubuntu 12.04, we highly recommend upgrading or migrating to a supported version of Ubuntu:

Reason: Ubuntu 12.04 reached end of life (EOL) on April 28, 2017 and no longer receives security patches or updates. This guide is no longer maintained.

See Instead: This guide might still be useful as a reference, but may not work on other Ubuntu releases. We strongly recommend using the following guide for working with Java on Ubuntu: How To Install Java with Apt-Get on Ubuntu 16.04.

Introduction


Having Java installed is a prerequisite for many articles and programs. This tutorial will guide you through the process of installing and managing different versions of Java on Ubuntu 12.04.

Installing default JRE/JDK


This is the recommended and easiest option. This will install OpenJDK 6 on Ubuntu 12.04 and earlier and on 12.10+ it will install OpenJDK 7.

Installing Java with apt-get is easy. First, update the package index:

   sudo apt-get update

Then, check if Java is not already installed:

java -version

If it returns “The program java can be found in the following packages”, Java hasn’t been installed yet, so execute the following command:

sudo apt-get install default-jre

This will install the Java Runtime Environment (JRE). If you instead need the Java Development Kit (JDK), which is usually needed to compile Java applications (for example Apache Ant, Apache Maven, Eclipse and [IntelliJ IDEA](http://www.jetbrains.com/idea/, etc.) execute the following command:

sudo apt-get install default-jdk

The JDK is usually only necessary if you are going to compile Java programs or if your software specifically requires it in addition to Java. Since the JDK contains the JRE, there are no disadvantages if you install the JDK instead of the JRE, except for the larger file size.

All other steps are optional and must only be executed when needed.

Installing OpenJDK 7 (optional)


To install OpenJDK 7, execute the following command:

sudo apt-get install openjdk-7-jre 

This will install the Java Runtime Environment (JRE). If you instead need the Java Development Kit (JDK), execute the following command:

sudo apt-get install openjdk-7-jdk

Installing Oracle JDK (optional)


The Oracle JDK is the official JDK; however, it is no longer provided by Oracle as a default installation for Ubuntu.

You can still install it using apt-get. To install any version, first execute the following commands:

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

Then, depending on the version you want to install, execute one of the following commands:

Oracle JDK 6


This is an old version but still in use.

sudo apt-get install oracle-java6-installer

Oracle JDK 7


This is the latest stable version.

sudo apt-get install oracle-java7-installer

Oracle JDK 8


This is a developer preview, the general release is scheduled for March 2014. This external article about Java 8 may help you to understand what it’s all about.

sudo apt-get install oracle-java8-installer

Managing Java (optional)


When there are multiple Java installations on your Droplet, the Java version to use as default can be chosen. To do this, execute the following command:

sudo update-alternatives --config java

It will usually return something like this if you have 2 installations (if you have more, it will of course return more):

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 now choose the number to use as default. This can also be done for the Java compiler (javac):

sudo update-alternatives --config javac

It is the same selection screen as the previous command and should be used in the same way. This command can be executed for all other commands which have different installations. In Java, this includes but is not limited to: keytool, javadoc and jarsigner.

Setting the “JAVA_HOME” environment variable


To set the JAVA_HOME environment variable, which is needed for some programs, first find out the path of your Java installation:

sudo update-alternatives --config java

It returns something like:

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 path of the installation is for each:

  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 preferred installation and then edit the file /etc/environment:

sudo nano /etc/environment

In this file, add the following line (replacing YOUR_PATH by the just copied path):

JAVA_HOME="YOUR_PATH"

That should be enough to set the environment variable. Now reload this file:

source /etc/environment

Test it by executing:

echo $JAVA_HOME

If it returns the just set path, the environment variable has been set successfully. If it doesn’t, please make sure you followed all steps correctly.

<div class=“author”>Submitted by: <a href=“http://koenv.com”>Koen Vlaswinkel</a></div>

Thanks for learning with the DigitalOcean Community. Check out our offerings for compute, storage, networking, and managed databases.

Learn more about our products

About the authors

Still looking for an answer?

Ask a questionSearch for more help

Was this helpful?
 
10 Comments


This textbox defaults to using Markdown to format your answer.

You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!

Thank you for very clear tutorial, jp

Very good tutorial… Thank you :)

Java 8 instructions are outdated

Andrew SB
DigitalOcean Employee
DigitalOcean Employee badge
April 30, 2014

@jformoso: What is the problem you’re seeing? What version of Ubuntu are you trying to install it on?

I was getting an error add-apt-repository: command not found

To fix it had to install sudo apt-get install software-properties-common

This was to install the oracle-jdk on an Ubuntu 12.10x64 droplet

Thank you from Rome, very good tutorial ;)

Thanks for the tutorial. I needed to add “-y” to the add-apt-repository.

Thanks for the tutorial. It is very needful. I set this JAVA_HOME path but I also want know "how to set a path for MAVEN? please guide me in this regard.

Andrew SB
DigitalOcean Employee
DigitalOcean Employee badge
June 12, 2014

@anandsbj1989: It depends on how you installed Maven. If you ran: <pre> sudo apt-get install maven2 </pre> Then you would use “/usr/share/maven2/”

amazing tutorial thanks.

Try DigitalOcean for free

Click below to sign up and get $200 of credit to try our products over 60 days!

Sign up

Join the Tech Talk
Success! Thank you! Please check your email for further details.

Please complete your information!

Become a contributor for community

Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.

DigitalOcean Documentation

Full documentation for every DigitalOcean product.

Resources for startups and SMBs

The Wave has everything you need to know about building a business, from raising funding to marketing your product.

Get our newsletter

Stay up to date by signing up for DigitalOcean’s Infrastructure as a Newsletter.

New accounts only. By submitting your email you agree to our Privacy Policy

The developer cloud

Scale up as you grow — whether you're running one virtual machine or ten thousand.

Get started for free

Sign up and get $200 in credit for your first 60 days with DigitalOcean.*

*This promotional offer applies to new accounts only.