Softpedia
 

NEWS CATEGORIES:



NEWS ARCHIVE >>
SOFTPEDIA REVIEWS >>
MEET THE EDITORS >>
TRENDING TODAY
Home > News > Linux > Ubuntu Tips and Tricks

November 26th, 2012, 22:21 GMT · By

How to Install Oracle Java in Ubuntu

SHARE:

Adjust text size:

Oracle Java
Enlarge picture
The following tutorial will teach all users of the Ubuntu operating system how to install the Java Runtime and SDK from Oracle on their machines.

By default, new Ubuntu installation comes with an open source implementation of Java SE installed, called OpenJDK (Open Java Development Kit), which is not compatible with applications written in Oracle Java.

In this quick guide, we will teach you how to remove OpenJDK from your Ubuntu system and install the Java Runtime Environment and Java Development Kit from Oracle.

We've tested this tutorial on a healthy Ubuntu 12.10 (Quantal Quetzal) operating system. It works with both 32-bit and 64-bit architectures.

Requirements? Just grab the latest JRE and JDK binary archives from Oracle. Open a terminal (either from the Dash or by hitting the CTRL+ALT+T key combination) and paste the following commands, one by one, hitting Enter after each one (input your password when asked):

sudo apt-get purge openjdk-\*

sudo mkdir -p /usr/local/java

cd ~/Downloads


For 32-bit users:


sudo -s cp -r jdk-7u9-linux-i586.tar.gz /usr/local/java

sudo -s cp -r jre-7u9-linux-i586.tar.gz /usr/local/java

cd /usr/local/java

sudo -s tar xvzf jdk-7u9-linux-i586.tar.gz

sudo -s tar xvzf jre-7u9-linux-i586.tar.gz


For 64-bit users:

sudo -s cp -r jdk-7u9-linux-x64.tar.gz /usr/local/java

sudo -s cp -r jre-7u9-linux-x64.tar.gz /usr/local/java

cd /usr/local/java

sudo -s chmod a+x jdk-7u9-linux-x64.tar.gz

sudo -s chmod a+x jre-7u9-linux-x64.tar.gz

sudo -s tar xvzf jdk-7u9-linux-x64.tar.gz

sudo -s tar xvzf jre-7u9-linux-x64.tar.gz


Now, both 32-bit and 64-bit users will need to run the following command:

sudo gedit /etc/profile


...and paste the following code at the end of the file:

JAVA_HOME=/usr/local/java/jdk1.7.0_09
PATH=$PATH:$HOME/bin:$JAVA_HOME/bin
JRE_HOME=/usr/local/java/jre1.7.0_09
PATH=$PATH:$HOME/bin:$JRE_HOME/bin
export JAVA_HOME
export JRE_HOME
export PATH


Save the file and close gedit. Continue pasting the following commands, one by one, hitting Enter after each one:

sudo update-alternatives --install "/usr/bin/java" "java" "/usr/local/java/jre1.7.0_09/bin/java" 1

sudo update-alternatives --install "/usr/bin/javac" "javac" "/usr/local/java/jdk1.7.0_09/bin/javac" 1

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

sudo update-alternatives --set java /usr/local/java/jre1.7.0_09/bin/java

sudo update-alternatives --set javac /usr/local/java/jdk1.7.0_09/bin/javac

sudo update-alternatives --set javaws /usr/local/java/jre1.7.0_09/bin/javaws

. /etc/profile


That's it! Oracle Java is now installed in your Ubuntu machine and you can run almost any Java written application.

If you have problems with the tutorial, do not hesitate to comment below!


19,742 hits · 11 comments
Link to this article · Print article · Send to friend

MUST-READ RELATED ARTICLES:


Installing Ubuntu 12.10

Ubuntu 13.04 Will Run on the Google Nexus 7 Tablet

Ubuntu 12.10 Has Been Officially Released

The Linux Games Available on Steam

Ubuntu Secured Remix 12.10 Is Based on Ubuntu 12.10

READER COMMENTS:


Comment #1 by: Fizzelle on 26 Nov 2012, 22:50 UTC reply to this comment

Aha! Wondered what I was doing wrong. This has now sorted it, thank you.


Comment #2 by: Linatux on 27 Nov 2012, 22:52 UTC reply to this comment

cd /home/"your_user_name"/Downloads could be simplified to 'cd ~/Downloads'


Comment #3 by: Carlos on 29 Nov 2012, 15:42 UTC reply to this comment

1. You don't need to delete the OpenJDK installation to install the Oracle Java.

2. You only need to download and install the JDK, not both the JRE and JDK.


Comment #4 by: Drew on 29 Nov 2012, 16:24 UTC reply to this comment

You shouldn't set your paths in /etc/profile as that is prone to being overwritten during a system or dist update / upgrade.

You should create a separate file: /etc/profile.d/java

You then need to make that file executable: chmod a+x

Your paths are also prone to being corrupted and can be made more manageable like this:
export JAVA_HOME=/usr/java/default
export PATH=$PATH:$JAVA_HOME/bin


Comment #5 by: thetoastman on 29 Nov 2012, 17:13 UTC reply to this comment

OK,

I have a few questions and comments even though I prefer running Fedora to Ubuntu.

1. Is there any reason to make a compressed tar archive executable?

I can't think of any.

2. Creating paths to installations with version numbers

This of course means that you'll have to go through your procedure every time you update Java. I admit, Java updates have slowed, but then you'll have to remember how to do this in 6 months when there's an update.

I suggest making the following modifications

a. create a directory structure under /opt
sudo mkdir -p /opt/Oracle/Java

This is normally the place vendor-supplied (as opposed to locally compiled) software is installed (/opt/[vendor]/[product])

b. After extracting the files, make soft links

sudo ln -s /opt/Oracle/Sun/jre1.7.0_9 /opt/Oracle/Sun/jre
sudo ln -s /opt/Oracle/Sun/jdk1.7.0_9 /opt/Oracle/Sun/java

c. Use the links in your /etc/profile modifications

JRE_HOME=/opt/Oracle/Java/jre
JAVA_HOME=/opt/Oracle/Java/java
PATH=$JRE_HOME/bin:$JAVA_HOME/bin:$PATH
export JRE_HOME JAVA_HOME PATH

I don't think you'll need to do the alternatives dance if you do the above. I've never had to do it with Fedora, but your mileage may vary on Ubuntu.

I particularly dislike the alternatives dance because I'm a developer (sometimes). I use many more commands than just javac, java, and javaws. I don't want to have to create the alternatives for each command.

However, if you do, then use the soft links (I'll show you why in a minute). For example:

sudo update-alternatives --install "/usr/bin/java" "java" \
"/opt/Oracle/Java/jre/bin/java" 1

Now it's time to upgrade. Here's how you do it.

1. Download the latest and greatest tar.gz from Oracle

2. Copy to /opt/Oracle/Java
sudo cp [latest-greatest] /opt/Oracle/Java

3. Go and uncompress it
cd /opt/Oracle/Java
sudo tar xvfz [latest-greatest]
sudo rm [latest-greatest]

4. Change the soft links
sudo rm /opt/Oracle/Java/jre
sudo rm /opt/Oracle/Java/java
sudo ln -s /opt/Oracle/Java/[latest-jre] /opt/Oracle/Java/jre
sudo ln -s /opt/Oracle/Java/[latest-jdk] /opt/Oracle/Java/java

Done!

No messing around with environment variables in /etc/profile, no messing around with alternatives (again), and the effect is immediate.

As a bonus, if you've linked the right Java plugin (in /opt/Oracle/Java/jre) to the proper plugins directory for your browser, then your browser plugin is automatically updated as well.

Also, if something goes horribly wrong, and you need to drop back to the previous version of Java . . .

cd /opt/Oracle/Java
sudo rm /opt/Oracle/Java/jre
sudo rm /opt/Oracle/Java/java
sudo ln -s /opt/Oracle/Java/[previous-jre] /opt/Oracle/Java/jre
sudo ln -s /opt/Oracle/Java/[previous-jdk] /opt/Oracle/Java/java

And you're back to the previous version.


Comment #6 by: leandro on 29 Nov 2012, 17:18 UTC reply to this comment

Really, this kind of post only drives people away from Linux. There are many easiest ways of doing that, one in particular that updates the Java instalation automatically. First, there is a comprehensive review on Java instalations in the standard Ubuntu Help page:

https://help.ubuntu.com/community/Java

I still do not understand why cannonical does not provide a "supported" PPA for this.

Comment #6.1 by: Marius Nestor on 30 Nov 2012, 14:05 GMT

Because it will be illegal. Moreover, I know for sure that Canonical will shut down all PPAs that distribute Oracle's Java binaries, as they violate their terms, and Oracles ones too!

So, please do NOT post any more PPAs.


Comment #7 by: Harish on 30 Nov 2012, 05:38 UTC reply to this comment

How awesome? What happened to portability? I thought oracle was defending against java like languages which prevents portability.


Comment #8 by: johnjoeallen on 30 Nov 2012, 10:02 UTC reply to this comment

No need to make the .tar.gz files executable

You should remove the following steps
sudo -s chmod a+x jdk-7u9-linux-x64.tar.gz
sudo -s chmod a+x jre-7u9-linux-x64.tar.gz


Comment #9 by: test on 30 Nov 2012, 17:05 UTC reply to this comment

Why would OpenJDK not be compatible?


Comment #10 by: Ram on 03 Jan 2013, 14:10 UTC reply to this comment

Perfect... Thank you!

Copyright © 2001-2013 Softpedia. Contact/Tip us at

WindowsGamesDriversMacLinuxScriptsMobileHandheldNews

SUBMIT PROGRAM   |   ADVERTISE   |   GET HELP   |   SEND US FEEDBACK   |   RSS FEEDS   |   UPDATE YOUR SOFTWARE   |   ROMANIAN FORUM