Sotirios-Efstathios Maneas

About Sotirios-Efstathios Maneas

Sotirios-Efstathios (Stathis) Maneas is a postgraduate student at the Department of Informatics and Telecommunications of The National and Kapodistrian University of Athens. His main interests include distributed systems, web crawling, model checking, operating systems, programming languages and web applications.

Install Java on Ubuntu tutorial

In this tutorial we will show you how to install the latest version of Java in Ubuntu 12.04 or later. An internet connection is required in order to download the corresponding Java packages.

There are different versions of Java that can be installed in Ubuntu. First of all, the OpenJDK project is an open-source implementation of the Java SE Platform. Second, Oracle Java 7 is Oracle’s own version of Java 7.0. Finally, you can download the latest version of Java by using the corresponding installer.

This tutorial applies to Ubuntu 12.04 and later for both Java 6.0 and Java 7.0.

 

OpenJDK

Java 6.0

In order to install the Java Runtime Environment (JRE), we must issue the following command:

sudo apt-get install openjdk-6-jre

Also, we need to install the IcedTea Web plugin, in order for Java applets to be executed, using the command:

sudo apt-get install icedtea6-plugin

Finally, in order to write and develop Java programs, we need the Java Development Kit (JDK). We can install it by executing the command:

sudo apt-get install openjdk-6-jdk

Notice that the installation of JDK includes the installation of JRE.

Java 7.0

In order to install the Java Runtime Environment (JRE), we must issue the following command:

sudo apt-get install openjdk-7-jre

Also, we need to install the IcedTea Web plugin, in order for Java applets to be executed, using the command:

sudo apt-get install icedtea7-plugin

Finally, in order to write and develop Java programs, we need the Java Development Kit (JDK). We can install it by executing the command:

sudo apt-get install openjdk-7-jdk

Notice that the installation of JDK includes the installation of JRE.

Oracle Java

First of all, we must remove any existing versions of OpenJDK/JRE. In order to achieve that, open a terminal and execute the following command:

sudo apt-get purge openjdk-\*

This command shall remove any installed package that belongs to OpenJDK/JRE. Then, we create a directory in our local machine that will store the Oracle Java:

sudo mkdir -p /usr/local/java

Once we complete these two steps, we must visit the Oracle’s official download page here and download the appropriate Java packages for our system. You can download both Java SE Development Kit 7 and Java SE Runtime Environment 7, but let me remind you that the Java Development Kit (JDK) also includes a Java Runtime Environment (JRE).

Assuming that the downloaded file is placed inside the Downloads folder and that the local architecture is 32-bit, we execute the following commands:

cd ~/Downloads
sudo mv jdk-7u45-linux-i586.tar.gz /usr/local/java
cd /usr/local/java

These commands move the downloaded file inside the newly created directory. In case your system’s architecture is 64-bit change the aforementioned commands appropriately.

Then, we extract the installation files from the archive:

sudo tar xvzf jdk-7u45-linux-x64.tar.gz

Once you have finished the aforementioned commands, feel free to remove the archive, in order to save some free space:

sudo rm jdk-7u45-linux-x64.tar.gz

The next step is to create the corresponding system variables. Execute the following command in order to open Ubuntu’s default editor

sudo gedit /etc/profile

and copy-paste the following lines at the end of the file:

JAVA_HOME=/usr/local/java/jdk1.7.0_45
PATH=$PATH:$HOME/bin:$JAVA_HOME/bin
JRE_HOME=/usr/local/java/jdk1.7.0_45/jre
PATH=$PATH:$HOME/bin:$JRE_HOME/bin

export JAVA_HOME
export JRE_HOME
export PATH

Save the /etc/profile file and exit. The final step is to inform the system about the installation location of Java. In order to achieve that, we execute the following commands:

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
sudo update-alternatives --install "/usr/bin/javaws" "javaws" "/usr/local/java/jre1.7.0_45/bin/javaws" 1

sudo update-alternatives --set java /usr/local/java/jdk1.7.0_45/jre/bin/java
sudo update-alternatives --set javac /usr/local/java/jdk1.7.0_45/bin/javac
sudo update-alternatives --set javaws /usr/local/java/jdk1.7.0_45/bin/javaws

Once all the aforementioned commands have been successfully executed, we reload the /etc/profile file, in order for our changes to take place:

. /etc/profile

Now, we can verify our installed version of Java as shown in the next section.

Oracle Java Installer

A very quick and simple way to download and install the latest version of Java in Ubuntu is running the following commands in a terminal:

sudo add-apt-repository ppa:webupd8team/java
sudo apt-get update
sudo apt-get install oracle-java7-installer

The first command adds a new entry to your repository, which contains the latest version of Java (currently 7u51). Then, the last command downloads and executes the Oracle’s Java installer. The newly added ppa provides the full Oracle Java Development Kit (JDK) 7 package.

Verify the installed version of Java

In order to verify which version of Java is installed in your system, open a terminal (Ctrl + Shift + T) and issue the following command:

java –version

A sample output is shown below:

java version "1.7.0_51"
Java(TM) SE Runtime Environment (build 1.7.0_51-b13)
Java HotSpot(TM) 64-Bit Server VM (build 24.51-b03, mixed mode)

Multiple Java versions

In case you have installed multiple Java versions, you can choose the default Java version of your system, using the command:

sudo update-alternatives --config java

 
This was a tutorial on how to install the latest version of Java in Ubuntu 12.04 or later.

Do you want to know how to develop your skillset to become a Java Rockstar?

Subscribe to our newsletter to start Rocking right now!

To get you started we give you two of our best selling eBooks for FREE!

JPA Mini Book

Learn how to leverage the power of JPA in order to create robust and flexible Java applications. With this Mini Book, you will get introduced to JPA and smoothly transition to more advanced concepts.

JVM Troubleshooting Guide

The Java virtual machine is really the foundation of any Java EE platform. Learn how to master it with this advanced guide!

Given email address is already subscribed, thank you!
Oops. Something went wrong. Please try again later.
Please provide a valid email address.
Thank you, your sign-up request was successful! Please check your e-mail inbox.
Please complete the CAPTCHA.
Please fill in the required fields.
Examples Java Code Geeks and all content copyright © 2010-2014, Exelixis Media Ltd | Terms of Use | Privacy Policy | Contact
All trademarks and registered trademarks appearing on Examples Java Code Geeks are the property of their respective owners.
Java is a trademark or registered trademark of Oracle Corporation in the United States and other countries.
Examples Java Code Geeks is not connected to Oracle Corporation and is not sponsored by Oracle Corporation.
Do you want to know how to develop your skillset and become a ...
Java Rockstar?

Subscribe to our newsletter to start Rocking right now!

To get you started we give you two of our best selling eBooks for FREE!

Get ready to Rock!
You can download the complementary eBooks using the links below:
Close