Java is one of the most popular programming languages used to build different types of applications and systems.
In this tutorial, we will explain how to install one or more Java (OpenJDK) versions on Ubuntu 18.04 and how to set the default Java via update-alternatives
command.
Installing OpenJDK 11
01- Run the following command as root or user with sudo
privileges to install Java 11:
$ sudo apt update $ sudo apt install default-jdk
02- Once the installation is complete, you can verify it by checking the Java version:
$ java -version openjdk version "11.0.7" 2020-04-14 OpenJDK Runtime Environment (build 11.0.7+10-post-Ubuntu-2ubuntu218.04) OpenJDK 64-Bit Server VM (build 11.0.7+10-post-Ubuntu-2ubuntu218.04, mixed mode, sharing)
Installing OpenJDK 8
01- If your application requires Java 8, so, you can install it by excuting the below command:
$ sudo apt update $ sudo apt install openjdk-8-jdk
02- Once the installation is complete, you can verify it by checking the Java version:
$ java -version openjdk version "1.8.0_252" OpenJDK Runtime Environment (build 1.8.0_252-8u252-b09-1~18.04-b09) OpenJDK 64-Bit Server VM (build 25.252-b09, mixed mode)
Setting the Default Java Version
So, if you installed multiple Java versions on your Ubuntu system, you can use the update-alternatives
command to set which Java version will be used by default.
01- To check what Java version is set as the default one, type:
$ java -version
02- If you want to change the default version, use the command alternatives
to list all the installed Java versions.
$ update-alternatives --config java There are 2 choices for the alternative java (providing /usr/bin/java). Selection Path Priority Status ------------------------------------------------------------ * 0 /usr/lib/jvm/java-11-openjdk-amd64/bin/java 1111 auto mode 1 /usr/lib/jvm/java-11-openjdk-amd64/bin/java 1111 manual mode 2 /usr/lib/jvm/java-8-openjdk-amd64/jre/bin/java 1081 manual mode Pressto keep the current choice[*], or type selection number:
03- Finally, enter the number of the version you want to use as the default and press Enter
.
Setting the JAVA_HOME Environment Variable
The JAVA_HOME
environment variable is used by some Java applications to determine the Java installation location and specify which Java version should be used to run the application.
01- To set the JAVA_HOME
variable, you can update the /etc/environment
file as below:
JAVA_HOME="/usr/lib/jvm/java-11-openjdk-amd64"
02- Finally, verify that the JAVA_HOME
environment variable was correctly set:
$ echo $JAVA_HOME /usr/lib/jvm/java-11-openjdk-amd64
Conclusion
You have successfully installed Java8 and Java 11 on your Ubuntu 18. You might want to check the following guides:
We hope this tutorial was enough Helpful. If you need more information, or have any questions, just comment below and we will be glad to assist you!