How to install JDK on Ubuntu

Installing Java JDK is easy and straightforward in Windows and OSX but not in Ubuntu. Here is how I am installing JDK on Ubuntu.
Download the latest JDK package from Oracle website. If you don’t know which file to choose you ought to select .tar.gz files whether 32bit or 64bit depends on your requirements.
after download the right package you need to extract it using following command you can extract it :

tar xvf jdk-8u161-linux-x64.tar.gz -C /opt/

Note : In this example I am using JDK version 8 revision 161

The /opt directory is where I chose to extract my JDK.

After extracting my JDK it is time to let Ubuntu know where to look after the Java Development Kit and Java Run Time.

Suppose you want to install your java and javac and javaws executable files in /usr/binĀ directory :

sudo update-alternatives --install /usr/bin/java java /opt/jdk1.8.0_161/bin/java 1
sudo update-alternatives --install /usr/bin/javac javac /opt/jdk1.8.0_161/bin/javac 1
sudo update-alternatives --install /usr/bin/javaws javaws /opt/jdk1.8.0_161/bin/javaws 1

After executing update-alternatives its time to set JAVA_HOME. Open /etc/environment file and add following lines to it :

JAVA_HOME="/opt/jdk1.8.0_161/"
export JAVE_HOME

Then type

source /etc/environment

now in command line if you type

java -version

you will see the result.

What does $? mean in linux?

The $? is called exit code of an application used to run. As an example type following commands in console:
top
then press CTRL-Z and then type:
echo $?
You will see 147 as the answer. Return values more than 128 usually means signal and you have to do the calculation by subtracting 128 from the returned result. In this case you will get 19 which means SIGSTOP

You can read about linux signals using :

man 7 signal