How to Install Java in Ubuntu 20.04 / 22.04

How to Install Java in Ubuntu 20.04 / 22.04. In this post, we introduce Java and after that we guide you through different ways to install Java on Ubuntu 20.04 and 22.04.

First and foremost, Java is considered one of the best and most commonly used programming languages worldwide. Firstly created in the middle of the 1990s. Well, it has since managed to grow to be a core component of the software development industry. Simply put, Java is an object oriented, concurrent, class based general purpose programming language. In this blog, we go through what Java is, how it works, and its advantages first and then we move onto install steps.

Let’s start this article blog about How to Install Java in Ubuntu 20.04 / 22.04.

What is Java?

Evidently, Java is a programming language used to develop software applications that run on several operating systems, including Windows, Linux, and Mac OS. Since it is designed to be portable, the same cod runs on several systems without the need for any adjustments or modifications. Being an object oriented language, Java is designed around the concept of objects that stores data and perform functions.

Additionally, Java has low level code known as bytecode that is processed on any platform, having a Java Virtual Machine (JVM). The JVM is a processor that reads the bytecode and converts it into operating system compatible machine code. Because of this, Java programs are platform independent and works on any system that has the JVM installed.

Advantages of Java

Platform Independence

As it was stated earlier, Java is a platform independent language. Hence, it operates unmodified on several platforms. Due to this, developers create code once and have it operate on any platform.

Object Oriented

Moreover, Java is an object oriented language, it is based on the concept of objects. So, any complicated programs are written and maintained with ease since the code is divided into manageable modules.

Memory Management

Programmers do not need to manually manage memory since Java offers automated memory management. The JVM manages memory allocation and deallocation, which limits the possibility of memory leaks and enhances the program’s overall efficiency.

Security

What is more, Java is a secure language to use since it contains built in security measures. For example, Java programs operate in a sandbox environment, which prohibits unauthorized access to system resources

Large Community

There is a large community of Java developers who are committed to enhancing the language and its libraries. This shows that there are a lot of internet resources for learning Java and resolving programming issues.

Multithreading

The Multithreading is a feature of Java that enables applications to carry out several activities at once. Programs that need to carry out numerous tasks simultaneously, such as web servers or database applications, may perform better as a result.

Easy to Learn

Another key point of Java, is it’s simplicity to learn when compared to other programming languages like C++ or Python. It’s a great language for beginners because of its simple, & clear syntax.

We have reached the installation part of How to Install Java in Ubuntu 20.04 / 22.04.

How to Install Java in Ubuntu 20.04 / 22.04

Firstly, we show you how to install Java OpenJDK and Oracle Java on Ubuntu 22.04 and 20.04. We also show you how to set the Java environment variable and switch between multiple Java versions.

Prerequisites

  • An Ubuntu 20.04 or 22.04 is installed on your system.
  • A root user or a user with sudo privileges.

Getting Started

Before doing anything, you need to update and upgrade all the packages to the latest version. Do so by running the following command.

				
					apt update -y
apt upgrade -y
				
			

After updating all the system packages, restart your system to implement the changes.

				
					reboot
				
			

Once you are done, please proceed to the next step.

Install Java from Default Repositories

By default, the Java package is included in the Ubuntu default repository. Please install any Java version easily with the apt command.

Albeit, to install Java 11 version, run the following command.

				
					apt install openjdk-11-jdk
				
			

After the installation, verify the Java version with the following command.

				
					java --version
				
			

Right now, you should see the Java version on the following screen.

If you want to install the older version of Java, run the following command.

				
					apt install openjdk-8-jdk
				
			

To install Java JDK 17, run the following command.

				
					apt install openjdk-17-jdk
				
			

In addition, also install the Java runtime environment with the following command.

				
					apt install default-jre
				
			

Once you installed all the Java versions, proceed to install Oracle Java in the next step.

Install Oracle Java

This section shows you how to install Oracle Java versions 17 and 11 on Linux.

Install Oracle Java 17

By default, the Oracle Java Debian package is available on the Oracle Java official website. Download it directly to your system with the following command.

				
					wget https://download.oracle.com/java/17/latest/jdk-17_linux-x64_bin.deb
				
			

Once the download is completed, install the downloaded file using the following command.

				
					apt install ./jdk-17_linux-x64_bin.deb
				
			

After installing Oracle Java, set Oracle Java as a default version with the following command.

				
					update-alternatives --install /usr/bin/java java /usr/lib/jvm/jdk-17/bin/java 100
				
			

Then check the Java version with the following command.

				
					java --version
				
			

This  shows you the Java 17 version in the following output.

				
					openjdk 17.0.6 2023-01-17
OpenJDK Runtime Environment (build 17.0.6+10-Ubuntu-0ubuntu122.04)
OpenJDK 64-Bit Server VM (build 17.0.6+10-Ubuntu-0ubuntu122.04, mixed mode, sharing)
				
			

Install Oracle Java 11

To install Oracle Java 11 version, you need to import the Java GPG key to your server. Import it with the following command.

				
					gpg --homedir /tmp --no-default-keyring --keyring /usr/share/keyrings/oracle-jdk11-installer.gpg --keyserver keyserver.ubuntu.com --recv-keys EA8CACC073C3DB2A
				
			

Next, add the Java repository to APT with the following command.

				
					echo "deb [arch=amd64 signed-by=/usr/share/keyrings/oracle-jdk11-installer.gpg] https://ppa.launchpadcontent.net/linuxuprising/java/ubuntu jammy main" | sudo tee /etc/apt/sources.list.d/oracle-jdk11-installer.list > /dev/null
				
			

Once the repository is added, update the repository cache with the following command.

				
					apt update
				
			

Next, create a directory for Oracle Java 11 with the following command.

				
					mkdir -p /var/cache/oracle-jdk11-installer-local/
				
			

Then, download the Oracle Java 11 version from the Oracle Java official website and copy it to the created directory.

				
					cp jdk-11.0.13_linux-x64_bin.tar.gz /var/cache/oracle-jdk11-installer-local/
				
			

Finally, install Oracle Java 11 with the following command.

				
					apt install oracle-java11-installer-local
				
			

During the installation, you are asked to accept the Oracle licence agreement as shown below.

Once Java is installed, check the new Java version using the following command.

				
					java --version
				
			

Change Default Java Version on Ubuntu

If you are a developer and working with many Java versions then you may often need to switch between Java versions. In this case, use the following command to set your preferred Java versions.

				
					update-alternatives --config java
				
			

Next, you are asked to select the Java version that you want to set as a default version as shown below.

Next, verify the default Java version with the following command.

				
					java -version
				
			

You should see the default Java versions in the following output.

				
					java 17.0.6 2023-01-17 LTS
Java(TM) SE Runtime Environment (build 17.0.6+9-LTS-190)
Java HotSpot(TM) 64-Bit Server VM (build 17.0.6+9-LTS-190, mixed mode, sharing)

				
			

Add Java Environment Variable

If you are developing a Java application then you may need to define a Java path in your application. In this case, you need to set the Java environment variable to define the Java path that you want to use with your application.

First, find the location of each Java version using the following command.

				
					update-alternatives --config java
				
			

You should see the Java path pf each installed version in the following output.

				
					Selection Path Priority Status
------------------------------------------------------------
0 /usr/lib/jvm/java-17-openjdk-amd64/bin/java 1711 auto mode
1 /usr/lib/jvm/java-11-openjdk-amd64/bin/java 1111 manual mode
2 /usr/lib/jvm/java-17-openjdk-amd64/bin/java 1711 manual mode
3 /usr/lib/jvm/java-8-openjdk-amd64/jre/bin/java 1081 manual mode
* 4 /usr/lib/jvm/jdk-17/bin/java 100 manual mode


				
			

Next, create an environment variable file with the following command.

				
					nano /etc/environment
				
			

Add the Java path as shown below.

				
					JAVA_HOME="/usr/lib/jvm/java-11-openjdk-amd64"
				
			

Then, activate the environment variable with the following command.

				
					source /etc/environment
				
			

Verify the added Java path using the following command.

				
					echo $JAVA_HOME
				
			

You show see the following output.

				
					/usr/lib/jvm/java-11-openjdk-amd64
				
			

Create a Sample Java Application

At this point, Java is installed on your server. Now, we guide you through creating a sample Java application and test it with the Java command line.

Let’s create a helloworld Java application using the following command.

				
					nano HelloWorld.java
				
			

Add the following code.

				
					public class HelloWorld { public static void main(String[] args) { System.out.println("👋 Hello World!"); } }


				
			

Save and close the file then run the Java application using the following command.

				
					java HelloWorld.java
				
			

Finally, you should see the output of the Java application on the following screen.

verify java application

Thank you for your time and effort in reading our article blog titled How to Install Java in Ubuntu 20.04 / 22.04. We shall now conclude the article. 

How to Install Java in Ubuntu 20.04 / 22.04 Conclusion

In this post, we showed you different ways to install Java JDK and Oracle Java on an Ubuntu server. Well, now you are able to choose your preferred way to install any Java version on your server. Lastly, Java is the best pick for software development, since it offers a wide range of features and benefits to function on major platforms without any modification. If you’re interested in learning Java, you shall easily seek guidance from experienced developers.

Avatar for Hitesh Jethva
Hitesh Jethva

I am a fan of open source technology and have more than 10 years of experience working with Linux and Open Source technologies. I am one of the Linux technical writers for Cloud Infrastructure Services.

0 0 votes
Article Rating
Subscribe
Notify of
0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x