Well, there is already a PPA which contains an installation script of Oracle Java on Ubuntu. This script automatically download the installer from Oracle's website, install it on the computer, and finally configure it as default JVM on Ubuntu. Please click here for further information about this PPA. Using installer script is the easiest way to get things done, but in case you don't trust the script which can damage you system, you can still install Oracle Java manually. You do all the stuff by yourself!

Downloading the binary
First thing first, please download the Oracle Java binary from Oracle's website. If you want to develop Java application, you should choose JDK, but if you want just run Java application, the JRE should be suitable for you.

Once the download button clicked, you will be offered with many choices of download. Please download the suitable version for your system architecture, and please make sure you select file which has *.tar.gz extension.

Installing on Ubuntu
Once the binary file downloaded, simply run the following command to install Oracle Java on Ubuntu:
  • sudo tar -zxvf jdk-7u7-linux-i586.tar.gz -C /usr/lib/jvm/
It's easy, isn't is? :D

Configuring default Java on Ubuntu
Once Oracle Java has been installed, you need to tell the system that you've just installed a new Java, and optionally make it as a default JVM on Ubuntu:
  • for alt in java javac; do sudo update-alternatives --install /usr/bin/$alt $alt /usr/lib/jvm/jdk*/bin/$alt 1000; done
Optionally, run the following command to choose the default Java on Ubuntu:
  • for alt in java javac; do sudo update-alternatives --config $alt; done
Check if the Oracle Java is set as default Java by typing:
  • java -version

In case, you are disappointed with Oracle Java and want to remove it (switch back to OpenJDK). Simply run the following command:
  • sudo update-alternatives --config java
  • #choose OpenJDK
  • sudo update-alternatives --config javac
  • #choose OpenJDK
  • sudo rm -rf /usr/lib/jvm/jdk1.7.0_07/
  • #It will remove the Oracle Java on Ubuntu
via: Xubuntu Geek