How to Install Java on Ubuntu

By | December 5, 2014

How to install Java on Ubuntu ? Installing Java on your system is one of a first things to do after you install your Ubuntu Linux. Many people have problems when installing Java, in fact it is very easy to do so. In this post I will show you how to install java on your Ubuntu Linux. This post will cover the installation for 32-bit and 64-bit version of Oracle Java. Java version used in this tutorial is 1.7.0_45, but don’t worry because almost of all java version use the same installation method. These instructions also work on Linux Mint, Elementary OS, and Debian. So how do we install our Oracle Java ? Just follow my instructions below.

OpenJDK, Oracle JDK, and JRE

First thing before we going to the installation progress, we should know what is  Open JDK, Oracle JDK, and JRE ? Let’s see the difference.

  • OpenJDK is a free and Open Source version of Oracle Java Development Kit as the result of an effor Sun Microsystems in 2006.
  • JDK stands for Java Development Kit, is a official version of JDK from Oracle while the OpenJDK is the open source version and this is what we need to install in this tutorial. JDK is needed when you have to do some development (programming).
  • JRE stands for Java Runtime Environment is what you need when you want to run Java based application.

Now, we know the difference, let’s begin the installation.

Step 1

Check if you have Java installed

To check your Java, you have to run the command from your terminal, so open your terminal and enter the following command :

$ java -version

If you see output :

java version "1.7.0_15"
OpenJDK Runtime Environment (IcedTea6 1.10pre) (7b15~pre1-0lucid1)
OpenJDK 64-Bit Server VM (build 19.0-b09, mixed mode)

Then you have wrong version of Java, you have to uninstall OpenJDK before continue the installation.

Step 2

Uninstall OpenJDK (Skip this step if OpenJDK isn’t installed)

If you not completely remove the OpenJDK/JRE from your system, this will make a conflict between the different vendor of java and make your system confused. To uninstall OpenJDK, open your terminal and enter the following command :

$ sudo apt-get purge openjdk-\*

Step 3

Download Oracle JDK/JRE for Linux

Go to Java Official site or just click this Link.

How to Install Java on Ubuntu - 1

How to Install Java on Ubuntu – 1

Then scroll down until you found License Agreement for Java SE, Accept the License Agreement.

Now select the download link for your Linux Version, don’t get confuse just download the tar.gz for Linux x64 or x86 depends on your Architecture (RPM is for RedHat Linux).

How to Install Java on Ubuntu - 2

How to Install Java on Ubuntu – 2

Now wait till the download is completed.

Step 4

Copy Oracle Java binaries

Before we copy our java binaries, we must create /usr/local/java directory, to do this, enter the following command :

$ sudo mkdir -p /usr/local/java

Then after creating the directory run this command to copy your java binaries :

For 32-bit and 64-bit

$ sudo cp jdk-7u45-linux-x64.tar.gz /usr/local/java/
$ cd /usr/local/java
$ ls
How to Install Java on Ubuntu - 3

How to Install Java on Ubuntu – 3

Step 5

Unpack Compressed Java binaries

Unpack your Compressed binaries to your /usr/local/java, use this following command to uncompressed :

For 32-bit and 64-bit

$ sudo tar -xvfz jdk-7u45-linux-x64.tar.gz
$ ls
How to Install Java on Ubuntu - 4

How to Install Java on Ubuntu – 4

Step 6

Edit system PATH file

Edit your system PATH file in /etc/profile and add some system variables to your system path, To edit your system path use any text editor, and open /etc/profile as root

$ sudo vim /etc/profile

After open up system path file, scroll down to the end of the file and add the following lines below to your system :

For JDK add this lines
JAVA_HOME=/usr/local/java/jdk1.7.0_45
JRE_HOME=$JAVA_HOME/jre
PATH=$PATH:$JAVA_HOME/bin:$JRE_HOME/bin
export JAVA_HOME
export JRE_HOME
export PATH
For JRE add this lines
JRE_HOME=/usr/local/java/jre1.7.0_45
PATH=$PATH:$JRE_HOME/bin
export JRE_HOME
export PATH

Save your new system path and exit.

How to Install Java on Ubuntu - 5

How to Install Java on Ubuntu – 5

Step 7

Tell your Ubuntu system where is your Oracle JDK/JRE is Located

Before using your java you should tell your system where is your Java is available for use, enter this following command to notifies your system that Java is available in your system and ready to use.

For JDK
$ sudo update-alternatives --install "/usr/bin/java" "java" "/usr/local/java/jdk1.7.0_45/jre/bin/java" 1
$ sudo update-alternatives --install "/usr/bin/javac" "javac" "/usr/local/java/jdk1.7.0_45/bin/javac" 1
For JRE Only
$ sudo update-alternatives --install "/usr/bin/java" "java" "/usr/local/java/jre1.7.0_45/bin/java" 1
For Oracle Java Web start (javaws)
$ sudo update-alternatives --install "/usr/bin/javaws" "javaws" "/usr/local/java/jdk1.7.0_45/bin/javaws" 1
or if you installing jre only, use the command below :

$ sudo update-alternatives --install "/usr/bin/javaws" "javaws" "/usr/local/java/jre1.7.0_45/bin/javaws" 1

After use those command, reload your system wide PATH by typing the following command

$ . /etc/profile
How to Install Java on Ubuntu - 6

How to Install Java on Ubuntu – 6

Last Step

Now Test your Oracle Java

Test your java to see if Java was installed correctly on your system, to test just run the following commands below, this command also work both for 32-bit and 64-bit system

Test your Java
$ java -version
Test your Java Compiler
$ javac -version

If you see output like the screenshot below, then congratulations your are successful installed Oracle Java on your Linux System.

How to Install Java on Ubuntu - 7

How to Install Java on Ubuntu – 7

Now before using your system and using Java, it’s recommended to restart your system, so your system will be fully configured.

So I hope this post will help you to “How to Install Java on Ubuntu”,  as I said before this tutorial is not only for Ubuntu, you can use this tutorial for Linux Mint, Elementary OS, and other Distributions that based on Ubuntu. If you have some suggestion or something to say about this post, you can leave it on the comment box below, thank you 🙂 .

Leave a Reply

Your email address will not be published. Required fields are marked *