Before installing Java, check first your operating system architecture to see if it's 32-bit or 64-bit. To do this open up a terminal and enter the following command:
The bit version of your operating system architecture will display whether it is 32-bit or 64-bit.
Then, download the Oracle Java JDK for Linux. Make sure you select the correct compressed binaries for your system architecture 32-bit or 64-bit (which end in tar.gz). You mustn't forget that 64-bit Java binaries do not work on 32-bit Linux operating systems, you will receive multiple system error messages, if you attempt to install 64-bit Java on 32-bit Ubuntu.
Copy the Java binaries into the /usr/local/java directory. In most cases, they are downloaded to: /home/"your_user_name"/Downloads.
Then, to install them, and assuming you're running on a 32-bit platform, run the following commands, one at a time:
After that, run the following command on the downloaded Oracle Java tar.gz files:
sudo -s cp -r jdk-7u15-linux-i586.tar.gz /usr/local/java
Unpack the compressed binaries, in the directory /usr/local/java
sudo -s chmod a+x jdk-7u15-linux-i586.tar.gz
Edit your system path file /etc/profile and add the following system variables to your system path. Use geany, nano, gedit or any other text editor as root, to open up /etc/profile.
sudo -s tar xvzf jdk-7u15-linux-i586.tar.gz
Once the file is open, scroll down to the end of it and add the following lines to the end of your /etc/profile file:
gksudo geany /etc/profile
Save the /etc/profile file and exit.
Now, you'll have to let your system know where your Java JDK/JRE is located:
Define your system's default Java:
sudo update-alternatives --install "/usr/bin/java" "java" "/usr/local/java/jdk1.7.0_15/bin/java" 1
sudo update-alternatives --install "/usr/bin/javac" "javac" "/usr/local/java/jdk1.7.0_15/bin/javac" 1
sudo update-alternatives --install "/usr/bin/javaws" "javaws" "/usr/local/java/jdk1.7.0_15/bin/javaws" 1
Finally, reload your system wide path /etc/profile by typing the following command:
sudo update-alternatives --set java /usr/local/java/jdk1.7.0_15/bin/java
sudo update-alternatives --set javac /usr/local/java/jdk1.7.0_15/bin/javac
sudo update-alternatives --set javaws /usr/local/java/jdk1.7.0_15/bin/javaws
Note that just after the reboot of your system /etc/profile will reload.
You compile and run your java programs at a terminal window, just like you would at a command-prompt window in Windows. The commands are exactly the same.