Installing the Default JRE/JDK
1,$sudo apt-get update
2,$sudo apt-get install default-jre
3,$sudo apt-get install default-jdk
Installing the Oracle JDK
1,$sudo add-apt-repository ppa:webupd8team/java
2,$sudo apt-get update
Oracle JDK 6 or 7 or 8 or 9
$sudo apt-get install oracle-java6-installer
$sudo apt-get install oracle-java7-installer
Managing Java
$sudo update-alternatives --config java
You can now choose the number to use as a default. This can also be done for other Java commands, such as the compiler (javac), the documentation generator (javadoc), the JAR signing tool (jarsigner), and more. You can use the following command, filling in the command you want to customize.
$sudo update-alternatives --config command
Setting the JAVA_HOME Environment Variable
Many programs, such as Java servers, use the JAVA_HOME environment variable to determine the Java installation location. To set this environment variable, we will first need to find out where Java is installed. You can do this by executing the same command as in the previous section:
$sudo update-alternatives --config java
Copy the path from your preferred installation and then open /etc/environment using nano or your favorite text editor.
$ sudo nano /etc/environment
At the end of this file, add the following line, making sure to replace the highlighted path with your own copied path.
/etc/environment
JAVA_HOME="/usr/lib/jvm/java-8-oracle"
Save and exit the file, and reload it.
$source /etc/environment
You can now test whether the environment variable has been set by executing the following command:
$echo $JAVA_HOME
This will return the path you just set.