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!

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

This great tutorial is now outdated. Currently the default version is 8, not 7. Can you please update it?

Had to try a few options to get a version of JDK - but all good.

Great tutorial - answered a lot of questions I had, all in one place.

Many thanks from a Linux noob!!!

unset [underScore]JAVA[underScore]OPTIONS JAVA[underScore]HOME=/usr/local/java/jdk1.8.0112 JRE[underScore]HOME=$JAVA[underScore]HOME/jre PATH=$PATH:$JAVA[underScore]HOME/bin:$JRE[underScore]HOME/bin export JAVA[underScore]HOME export JRE[underScore]HOME export PATH

so in this web site under score got ignore so for some help i again put it like: [underScore] so please keep it on mind and stay blessed

hi guy’s

here in this i am writing some simple step to install JAVA JDK AND JRE

first just download jdk-8u112-linux-x64.tar.gz and jre-8u112-linux-x64.tar.gz

Note: here i am writing Linux command what ever i did in my system, instead of my name ‘amit’ e.g. user name in my Linux system you can write your system user name, that only change you also have to do.

so first command to go for root priviladge:

Step 1: Type

amit@Deepin:~$sudo -i [sudo] password for amit: ******* <------ Your system password {after this you will reach into root priviledge} Step 2: create one folder where you would like to copy your both JDK and JRE software, In my case i place my software in /usr/local/java directory:

so after downloading go into downloading directory folder mostly it will be: /home/amit/Downloads <---- here amit is my user name, in your case that name will be your’s

to know this go on this, go on downloading area:

root@Deepin:~# cd /home/amit/Downloads root@Deepin:~# ls <----- to see weather jdk and jre package is avaliable jre-8u112-linux-x64.tar.gz jdk-8u112-linux-x64.tar.gz

ok it seems it avaliable,copy this both jdk and jre into your desire system location:

Step 3: root@Deepin:~# mkdir /usr/local/java root@Deepin:~#cp jre-8u112-linux-x64.tar.gz /usr/local/java root@Deepin:~#cp jdk-8u112-linux-x64.tar.gz /usr/local/java

after completing this:

Step 4:

root@Deepin:~#cd /usr/local/java root@Deepin:~#tar zxvf jre-8u112-linux-x64.tar.gz root@Deepin:~#tar zxvf jdk-8u112-linux-x64.tar.gz

{from here i am not writing more about what you have to do just you copy and pest cmd only edit your user name}

root@Deepin:~#ls jdk1.8.0_112 jdk-8u112-linux-x64.tar.gz jre1.8.0_112 jre-8u112-linux-x64.tar.gz

Step 5:

root@Deepin:~# gedit /etc/profile

{ from the bottom you just copy and pest below, Note: kindly don’t delete any thing form this page,after fi type enter and just pest the thing whatever i am writen}

unset _JAVA_OPTIONS JAVA_HOME=/usr/local/java/jdk1.8.0_112 JRE_HOME=/usr/local/java/jre1.8.0_112 PATH=$PATH:$JAVA_HOME/bin:$JRE_HOME/bin export JAVA_HOME export JRE_HOME export PATH

					{OR if only JDK installation Require}

unset _JAVA_OPTIONS JAVA_HOME=/usr/local/java/jdk1.8.0_112 JRE_HOME=$JAVA_HOME/jre PATH=$PATH:$JAVA_HOME/bin:$JRE_HOME/bin export JAVA_HOME export JRE_HOME export PATH

now just save and close page: {Note: some time some people are getting JAVA Pickedup _JAVA_OPTIONS: they can pest this ‘unset _JAVA_OPTIONS’ in first line}

after this perform some cmd in terminal i.e:

root@Deepin:~# update-alternatives --install “/usr/bin/java” “java” /usr/local/java/jre1.8.0_112/bin/java" 1 root@Deepin:~# update-alternatives --install “/usr/bin/javac” “javac” /usr/local/java/jdk1.8.0_112/bin/javac" 1 root@Deepin:~# update-alternatives --install “/usr/bin/javaws” “javaws” /usr/local/java/jre1.8.0_112/bin/javaws" 1 root@Deepin:~# update-alternatives --set java /usr/local/java/jre1.8.0_112/bin/java root@Deepin:~# update-alternatives --set javac /usr/local/java/jdk1.8.0_112/bin/javac root@Deepin:~# update-alternatives --set javaws /usr/local/java/jre1.8.0_112/bin/javaws root@Deepin:~# . /etc/profile root@Deepin:~#

{now you can check java version, JAVA HOME, and JRE Home. PATH}

root@Deepin:~# echo $HOME /root root@Deepin:~# echo $PATH /u01/app/oracle/product/11.2.0/xe/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/local/java/jdk1.8.0_112/bin:/usr/local/java/jre1.8.0_112/bin

root@Deepin:~# echo $JAVA_HOME /usr/local/java/jdk1.8.0_112 root@Deepin:~# echo $JRE_HOME /usr/local/java/jre1.8.0_112

root@Deepin:~# java -version java version “1.8.0_112” Java™ SE Runtime Environment (build 1.8.0_112-b15) Java HotSpot™ 64-Bit Server VM (build 25.112-b15, mixed mode)

	{Best Of Luck Guys's}

Nice one and its very useful.Thank you :)

I have a problem with my Ubuntu 11.04. It can’t download anything from terminal and Ubuntu software center. And system updates.

You are amazing !

dear publisher why this login page is displaying?

When I am typing in terminal screen sudo apt-get install openjdk-7-jdk Then it will print like- E: Unable to locate package openjdk-7-jdk

Now what I can do??

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.