Install Java on Ubuntu with Apt-Get. Learn how to Install Java on Ubuntu using Terminal, using WebUpd8 Team Oracle Java (JDK) 7/8/9 PPA. OpenJDK is a free and open source implementation of the Java programming language.
Install OpenJDK
OpenJDK is a free and open source implementation of the Java programming language. It has support from the Oracle Corporation and serves as the reference implementation of Java SE (Standard Edition). It is the basis for the Oracle Java products. Some additional information about OpenJDK is at: http://openjdk.java.net/.
Run the following commands in Terminal to install OpenJDK on Ubuntu:
Installing JDK 9
sudo apt-get install openjdk-8-jre
Installing JDK 8
sudo apt-get install openjdk-8-jre
Installing JDK 7
sudo apt-get install openjdk-7-jre
Please note that the openjdk-jre package contains just the Java Runtime Environment. If you want to develop Java programs then you will have to install the openjdk-jdk package.
Install Oracle JDK/Java
Oracle JDK/Java is a proprietary implementation of the Java programming language from the Oracle Corporation. It is based on the OpenJDK with the addition of some closed-source components. Canonical no longer packages Oracle Java for Ubuntu due to licensing restrictions, but some older packages may require that the Oracle JDK be installed.
Ubuntu Linux users can install Oracle JDK/Java using WebUpd8 Tea: Oracle Java (JDK) 7/8/9 PPA. Run the following commands to install Oracle JDK/Java:
sudo add-apt-repository ppa:webupd8team/java
sudo apt update
sudo apt install oracle-java8-installer
To install Java 9, simply replace sudo apt install oracle-java8-installer with sudo apt install oracle-java9-installer.
Once installed, run the command below to check the version of Java:
javac -version
Setting Java Environment Variables
Installing Java isn’t complete without setting Java environment variables. To set Java environment variables, run the command:
sudo apt install oracle-java8-set-default
For Java 9, replace oracle-java8-set-default with the oracle-java9-set-default.
Using Multiple Java Versions
Users can easily have multiple Java packages installed on a single Ubuntu machine. Ubuntu Linux uses the “alternatives” system to select a default Java package between multiple packages. To find the current setting for Java, run the following command:
update-alternatives --display java
If you wish to set a fixed alternative, do it by running this command:
update-alternatives --config java