Java logo

How to Install Oracle Java JDK on Ubuntu

Every version of Ubuntu for Desktop either comes with OpenJDK as the only Java compiler installed or no Java compiler at all (like Ubuntu 14.04). Most users don’t even notice this because OpenJDK meets their needs. A key reason for this default setup is that Oracle requires a user to agree to a special license before he/she can install and use their JDK (you’ll see this when downloading their JDK archive below). On the other hand, OpenJDK is licensed as free and open-source software under the GNU Public License. Now, there are tasks that aren’t very compatible with OpenJDK and will require you to install Oracle Java on your computer. (Good examples are compiling Android AOSP from source or developing Android apps.) So, I’ll show you how to remove OpenJDK from and install Oracle Java on your Ubuntu computer. The problem, though, is that Oracle Java is not available by default in the Ubuntu Software Center. So we will be downloading it directly from the Oracle website and installing it (as opposed to using some PPA from the Internet). This choice ensures that we can always get the latest version of Java by ourselves without adding an external PPA to our Ubuntu machine.

So, the installation steps can be broken down into two parts:

A. Removing OpenJDK from your Ubuntu machine (optional).

B. Installing Oracle Java instead.

Part A: Removing OpenJDK

This part is actually easy. All you need to do is open a terminal (e.g by pressing Ctrl + Alt + T on Ubuntu) and enter the following command:

java -version

The output from the above will tell you whether you have OpenJDK or not. If you don’t, proceed to Part B. If you do, enter this command to uninstall it:

sudo apt-get purge open-jdk*

apt-get will then remove OpenJDK and its dependencies from your computer.

 

Part B: Installing Oracle Java

In this ongoing tutorial, we’ll be installing Oracle Java 8. However, these instructions are also relevant to installing Oracle Java 7 if that’s what you need.

 

Step 1: Download Oracle Java.

Visit this page to download the latest version of Java for Linux/Ubuntu, which should be a compressed file of type tar.gz. Ensure that it’s appropriate for your 32-bit or 64-bit operating system. I use a 64-bit version of Ubuntu, so I downloaded the appropriate, most recent archive, which is jdk-8u121-linux-x64.tar.gz.

 

Step 2: Extract the archive

Let’s assume you saved the archive from the step above to your desktop with a name of jdk-8u121-linux-x64.tar.gz. Open a terminal and enter the following commands:

cd ~/Desktop

tar -xzf jdk-8u121-linux-x64.tar.gz java8

Note that if you didn’t save the archive to your desktop, you will need to use the appropriate path for cd, instead of using ~/Desktop.

PRO TIP: In the above, ~ is an Ubuntu (and Linux) shortcut for /home/USERNAME_OF_CURRENT_USER. So, the absolute path is something like /home/USERNAME_OF_CURRENT_USER/Desktop.

 

Step 3: Move the extracted directory to a place with restricted permissions

The Java JDK is a very important piece of software. You don’t want any program on your computer (like Bleachbit) or even yourself to modify or delete its folder accidentally. So, you should move your JDK to a directory with restricted permissions on your Ubuntu computer, most advisably somewhere in your root (/) directory. In my case, I moved it into /usr/local/. This choice was influenced by the fact that I installed Android SDK and Android Studio in that directory. Most users, however, prefer to move it to their /usr/bin or /usr/lib directory. So it’s up to you to decide what to do. If you decide to follow my example though, enter this command:

sudo mv java8/ /usr/local/

 

Step 4: Add Java to your path

Next, you need to add the location of the directory above to your computer’s PATH. That way, anytime the java command is given, the operating system will be able to find the software to run. So let’s say you moved your extracted JDK to /usr/local/java8. Now, open your File Manager and navigate to your home directory. Look around for a file named .profile (if you don’t see it initially, press Ctrl + H). Now, open that file in your favorite text editor (like Gedit) and find the line where your PATH variable is defined. The line should look like:

PATH="$PATH:/home/lagbaja/.bin"

You need to either prepend or append the location of the core Java programs to the above PATH. So, change it to:

PATH="$PATH:/usr/local/java8/jdk1.8.0_121/bin:/usr/local/java8/jre1.8.0_121/bin:/home/lagbaja/.bin"

Note how each location in the PATH variable must be separated with a colon (i.e :). Save the edited file and close the text editor.

WARNING: Make sure there is no trailing colon (i.e :) at the start or end of the string for your PATH above. Otherwise, this trailing colon will make Bash to look in the current directory for any file with the same name as the command it was given. This will give malicious people a way to hack your computer.

How to install Oracle Java on Ubuntu- Do not leave a dangling, orphaned colon at the beginning or end of your PATH! It is a security risk! Here are two bad examples and one good example of how to modify your PATH in order to install Oracle Java.

 

Step 5: Reboot your computer

Now that you’ve added the location of the JDK to your path, you need to reboot your Ubuntu computer. This will enable the system to reload the PATH variable while it is booting. Afterwards…

 

Step 6: Verify your installation

To verify that Oracle Java was properly installed on your computer, just type in the following command:

java -version

Unlike before, the output of this command should now indicate that you have Oracle Java installed.

That’s it! In order to update your Java installation just repeat the steps in Part B. And remember: using this method means you’re in charge of your own destiny. You also don’t get exposed to the risk of adding PPAs that aren’t well maintained or/and might have security issues. Though not all PPAs on the Internet are bad, you need to be careful about which of them you add to your Ubuntu computer.

Gracias por leer!

 

Leave a Reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.