I start this assuming that you have correctly installed java in your machine. If not have a look at part 1 post to correctly install java in your machine or in a remote server.
To do this task through terminal, you will need vi or vim editor installed in your machine. If not do the following in the terminal.
1.First type apt-get command to install vim as following.
sudo apt-get install vim
Give password credentials and get installed vim.
Now you can use vi commands in your terminal.
The only thing you have to do to set the java path is editing .bashrc file and set JAVA_HOME.
2. Type the following command to open up .bashrc file.
3. Go down to the bottom of the file and press the key "i" to ge the file editable.
4. Type following lines at the bottom of the file.
These lines will add JAVA_HOME variable to the system properties.
/usr/lib/jvm/java-6-oracle is the path to JAVA in my machine. This may vary according to the version that you have installed in your machine.
Set the PATH to the bin folder of JAVA. Export both variables.
5. press Esc to go back from the edit mode of the .bashrc file
6. press :w for changes to be written. Then the changes to the file will be saved.
7. pres :q! to quit the fie.
8. Now type the following command in the terminal to verify (print) the JAVA_HOME.
It will print JAVA_HOME.
9. Type the following command in the terminal to verify (print) the PATH.
It will print PATH.
You have successfully configured the java path in your machine now.