How to use Java on Ubuntu - Complete Guide

Java (JDK and GRE) is one of the most essential group of software every computer machine got to have. "Java can be found everywhere". You need Java to run a Java program on your machine, to run Java based web applications, to develop  Java applications for desktop, mobiles and tablets and for many other uses. It is very easy to install and use Java on Windows but if you are a die hard open source fan like me and you use Ubuntu, things can become tough for you. This guide will help you out. After reading this post, you will be able to install many versions of Java on your Ubuntu Linux system.



Available Versions


You can use Java on Ubuntu in many forms. The versions we are including in this how-to guide are:

  • Open JDK - Open JDK (and JRE) is a open source form of the original Oracle (Sun) Java JDK and JRE. This is the default Java form which is Ubuntu supports. You don't have to go through much pain if you prefer to choose Open JDK as it is the simplest to install Java version on Ubuntu.

  • Java 6 - The last version of Java developed by Sun Microsystems. Since Oracle took over Sun, this version of Java had some license issues for which it is not included in Ubuntu software repository, but you can still install this version manually following this guide.

  • Java 7 - The latest version of Java released under the banner of Oracle, can be installed but this version is not recommended by Ubuntu.


Installation Guides


Open JDK


Java Development Kit


You can install Open JDK Java development kit directly on your Ubuntu system by clicking here. Open JDk can also be downloaded from the official website or the Ubuntu Software Center.

Java Runtime Environment


You can install Open JDK Java runtime environment directly on your Ubuntu system by clicking here. Open JDk can also be downloaded from the official website or the Ubuntu Software Center.

Browser Plugin


For using Java based web applications on your web browser, you need a java browser plugin. You can install the iced tea browser plugin directly from here. You can also download it from the official website or Ubuntu Software Center.

Oracle (Sun) Java 6 and 7


The following steps will guide you to install Oracle (Sun) java 6 or 7. We have included steps for installing both JDK and JRE. If you don't want to install any one of these, please ignore the respective steps. Also the name of package files may differ according to the type of file you download. In this tutorial, I have used java-7u4-linux-x64.tar.gz, please change your file name in the commands accordingly.

  1. Open terminal by Ctrl+Alt+T.

  2. To check if you have already installed Java on your system, enter:
    java-version

  3. If you have any other version of  Java already installed on your system, you have to first remove it to avoid system conflicts. To remove Open JDK, enter:
    sudo apt-get autoremove openjdk-6-jre
    sudo apt-get autoremove openjdk-6-jdk

  4. Now create a default directory for installing Java in the usr/local folder. You cannot do this in the Ubuntu GUI, so enter:
    sudo mkdir -p /usr/local/java

  5. Download your preferred versions of JDK, JRE or both from the Oracle website. Make sure you choose the Linux versions packed in a tar.gz archive.

  6. Move the downloaded files to the directory you created in step 4. If you have downloaded the files in your downloads folder, use the path as follows, if you are using any other folder to download, either move the files in downloads folder first or change the path in your commands. Also, replace "username" with the username of your system.
    sudo -s
    cd /home/username/Downloads
    cp -r jdk-7u4-linux-x64.tar.gz /usr/local/java
    cp -r jre-7u4-linux-x64.tar.gz /usr/local/java
    cd /usr/local/java

  7. To make your downloaded files executable for all users on your system, run the following command as root user:
    sudo chmod a+x jdk-7u4-linux-x64.tar.gz
    sudo chmod a+x jre-7u4-linux-x64.tar.gz

  8. Extract the files from your downloaded package:
    tar xvzf jdk-7u4-linux-x64.tar.gz
    tar xvzf jre-7u4-linux-x64.tar.gz

  9. Open your system PATH file:
    sudo gedit /etc/profile
    and add the following lines at the bottom of the file:
    JAVA_HOME=/usr/local/java/jdk1.7.0_04
    PATH=$PATH:$HOME/bin:$JAVA_HOME/bin
    JAVA_HOME=/usr/local/java/jre1.7.0_04
    PATH=$PATH:$HOME/bin:$JAVA_HOME/bin
    export JAVA_HOME
    export PATHSave the file and close gedit.

  10. Change the location of Java in your system:
    sudo update-alternatives --install "/usr/bin/java" "java" "/usr/local/java/jre1.7.0_04/bin/java" 1
    sudo update-alternatives --install "/usr/bin/javac" "javac" "/usr/local/java/jdk1.7.0_04/bin/javac" 1

  11. Make the installed java version as default:
    sudo update-alternatives --set java /usr/local/java/jre1.7.0_04/bin/java
    sudo update-alternatives --set javac /usr/local/java/jdk1.7.0_04/bin/javac

  12. Reload your PATH file:
    /etc/profile

  13. Now check your installed java version again. This time you should see this installed version:
    java-version

  14. Reboot.

This is it. The next time your system starts, you will have your Oracle (Sun) Java version installed, but still this wouldn't work for web browsers. To use Java based web applications, install the iced tea plugin as mentioned above. This is all in this post. Thanks for reading.

References:


Comments