If you are using Ubuntu or any other Debian flavored Linux, this is the best way to setup Java. Open Terminal and just follow the below steps:
1. First add this remote repository-
$ sudo add-apt-repository ppa:webupd8team/java
2. Update your local system repo.
$ sudo apt-get update
3. Now install Java.
$ sudo apt-get install oracle-java8-installer
This will take some time to install. Better go outside for some fresh air. Have a cup of coffee…
4. If installation completed successfully, check the version.
$ java -version
5. Make the recently installed Java version default.
$ sudo apt-get install oracle-java8-set-default
6. Later on you can set any Java version as default, as per your requirement.
$ sudo update-alternatives –config java
This command will shows you up all the available Java versions installed in your system. Just specify the number you want to set or press Enter to go with the current one. Sometimes it require system restart to reflect the changes. So better do it for once, if require.