<\/span><\/h2>\r\n\r\n\r\n\r\nJava 8 is the previous and widely used Java LTS version. If your application built on Java 8, you can install it with the following command:<\/p>\r\n\r\n\r\n\r\n
apt-get install openjdk-8-jdk -y<\/pre>\r\n\r\n\r\n\r\nOnce installed, verify the Java version using the following command:<\/p>\r\n\r\n\r\n\r\n
java -version<\/pre>\r\n\r\n\r\n\r\nYou should get the following output:<\/p>\r\n\r\n\r\n\r\n
openjdk version \"1.8.0_275\"\r\nOpenJDK Runtime Environment (build 1.8.0_275-8u275-b01-0ubuntu1~20.04-b01)\r\nOpenJDK 64-Bit Server VM (build 25.275-b01, mixed mode)\r\n<\/pre>\r\n\r\n\r\n\r\n<\/span>Install Java 11<\/span><\/h2>\r\n\r\n\r\n\r\nJava 11 is the latest LTS version of Java. BY default, it ships in the Ubuntu 20.04 default repository. You can install it with the following command:<\/p>\r\n\r\n\r\n\r\n
apt-get install openjdk-11-jdk -y<\/pre>\r\n\r\n\r\n\r\nOnce Java 11 is installed, you can verify it with the following command:<\/p>\r\n\r\n\r\n\r\n
java -version<\/pre>\r\n\r\n\r\n\r\nYou should get the following output:<\/p>\r\n\r\n\r\n\r\n
openjdk version \"11.0.9.1\" 2020-11-04\r\nOpenJDK Runtime Environment (build 11.0.9.1+1-Ubuntu-0ubuntu1.20.04)\r\nOpenJDK 64-Bit Server VM (build 11.0.9.1+1-Ubuntu-0ubuntu1.20.04, mixed mode, sharing)\r\n<\/pre>\r\n\r\n\r\n\r\n<\/span>Install Oracle Java 15<\/span><\/h2>\r\n\r\n\r\n\r\nBy default, Oracle Java is not available in the Ubuntu 20.04 default repository. You will need to add the third-party repository in your system in order to install Oracle Java.<\/p>\r\n\r\n\r\n\r\n
First, install the required dependencies with the following command:<\/p>\r\n\r\n\r\n\r\n
apt-get install gnupg2 software-properties-common -y<\/pre>\r\n\r\n\r\n\r\nOnce all the dependencies are installed, add the Java repository with the following command:<\/p>\r\n\r\n\r\n\r\n
add-apt-repository ppa:linuxuprising\/java<\/pre>\r\n\r\n\r\n\r\nWhen the repository is added, you can install it with the following command:<\/p>\r\n\r\n\r\n\r\n
apt-get install oracle-java15-installer -y<\/pre>\r\n\r\n\r\n\r\nOnce the installation has been finished, you can verify it with the following command:<\/p>\r\n\r\n\r\n\r\n
java -version<\/pre>\r\n\r\n\r\n\r\nYou should get the following output:<\/p>\r\n\r\n\r\n\r\n
java version \"15.0.1\" 2020-10-20\r\nJava(TM) SE Runtime Environment (build 15.0.1+9-18)\r\nJava HotSpot(TM) 64-Bit Server VM (build 15.0.1+9-18, mixed mode, sharing)\r\n<\/pre>\r\n\r\n\r\n\r\n<\/span>Switch Between Different Java Versions<\/span><\/h2>\r\n\r\n\r\n\r\nIf you have multiple versions of Java installed in your system, you can easily set the default version using the update-alternatives command.<\/p>\r\n\r\n\r\n\r\n
To set the Java default version from Oracle Java 15 to Java 8, run the following command:<\/p>\r\n\r\n\r\n\r\n
update-alternatives --config java<\/pre>\r\n\r\n\r\n\r\nYou should see a list of all installed Java versions in the following output:<\/p>\r\n\r\n\r\n\r\n
There are 3 choices for the alternative java (providing \/usr\/bin\/java).\r\n\r\n Selection Path Priority Status\r\n------------------------------------------------------------\r\n 0 \/usr\/lib\/jvm\/java-11-openjdk-amd64\/bin\/java 1111 auto mode\r\n 1 \/usr\/lib\/jvm\/java-11-openjdk-amd64\/bin\/java 1111 manual mode\r\n* 2 \/usr\/lib\/jvm\/java-15-oracle\/bin\/java 1091 manual mode\r\n 3 \/usr\/lib\/jvm\/java-8-openjdk-amd64\/jre\/bin\/java 1081 manual mode\r\n\r\nPress to keep the current choice[*], or type selection number: 3\r\n<\/pre>\r\n\r\n\r\n\r\nType 3 and hit Enter to set Java 8 as the default Java version. You should get the following output:<\/p>\r\n\r\n\r\n\r\n
update-alternatives: using \/usr\/lib\/jvm\/java-8-openjdk-amd64\/jre\/bin\/java to provide \/usr\/bin\/java (java) in manual mode\r\n<\/pre>\r\n\r\n\r\n\r\n