How to Install Java on Ubuntu and Remove it When You’re Done
In the world of software development, Java is one of the most widely used programming languages. You can use it for developing websites, desktop software, android applications, and even games.
But before all that, the first thing you need to do is install Java on your machine. Most of the Linux distributions do not have Java preinstalled and users have to install it manually on their system.
In this article, we will discuss how to install and remove Java on Ubuntu, along with a detailed section on JDK and JRE.
JDK vs JRE: Which One Should You Choose?
When you first start with Java, abbreviations such as JDK, JRE, and JVM will confuse you the most.
JDK (Java Development Kit)
JRE (Java Runtime Environment)
JVM (Java Virtual Machine)
The primary difference between JDK and JRE is that JRE is used to run applications built on Java, while JDK is used to develop Java applications. JRE includes JVM and some class libraries that allow your system to run Java applications. JVM acts as a virtual machine that creates an environment on your computer for running Java.
On the other hand, JDK includes JRE and some additional development tools that allow you to use the class libraries for developing your own applications.
All in all, if you only want to run Java applications on your system, like playing Minecraft, you'll need the Java Runtime Environment. If your end goal is development and you want to dip your hands into programming, then the Java Development Kit is required.
How to Install Java on Ubuntu
Now that you've decided what you want to download, it is time to get practical and start installing Java on your Ubuntu machine.
Java has several platforms that are available to download.
Java Standard Edition (Java SE)
Java Micro Edition (Java ME)
Java Enterprise Edition (Java EE)
For this article, we will be downloading the standard edition of Java, i.e. Java SE.
Just like the different Java platforms, there are several Java implementations that you can install on your system. Ubuntu repositories provide an open-source Java implementation known as Open JDK and Open JRE. Oracle Java is another implementation that you can download, however, it's mostly used commercially.
Since the current LTS (Long-Term Support) version of Java is Java 11, this guide will focus on the same. You are free to download other versions of Java on your system if you want.
Related: Learn How to Create Classes in Java
Install OpenJDK and OpenJRE
OpenJDK can be installed on Ubuntu using the terminal. Use Apt for this process, which is the default package manager that comes preinstalled in Debian-based distributions.
Launch the terminal by pressing Ctrl + Alt + T on the keyboard.
Update your system's package list.
sudo apt-get update
Issue the following command in order to download OpenJDK.
sudo apt install default-jdk
Type in y and press Enter to confirm the installation.
Similarly, you can install Open JRE on your system as well:
sudo apt install default-jre
Verify if Java was successfully installed on your system by typing java --version in the terminal. If the output displays version information associated with the Java packages, then Java is successfully installed on your system.
Download and Install Oracle Java
The Ubuntu repositories do not provide the official Oracle Java package. Therefore, you will have to add third-party repositories to download it.
Open up the terminal by pressing Ctrl + Alt + T.
Enter the following command to get the add-apt-repository utility on your system.
sudo apt install software-properties-common
There are two repositories that provide the Oracle Java package. Add any of the two PPA repositories to your system.
sudo add-apt-repository ppa:webupd8team/java
sudo add-apt-repository ppa:linuxuprising/java
Update your system's package list using apt.
sudo apt-get update
Install Oracle Java 11 on your system.
sudo apt install oracle-java11-installer-local
You can download the Oracle Java package from the official Oracle website as well. To do so:
Head over to the Oracle Java Download webpage.
Scroll down till you find the Java SE 11 (LTS) download section.
Click on the JDK Download option.
Download the Linux x64Debian Package from the list of available downloads.
Check the I reviewed and accept the Oracle Technology Network License Agreement for Oracle Java SE option.
Click Download to get the package.
Note that the website will ask you to sign in to your Oracle account. If you don't have an account, you can create one for free. As soon as you log in to your account, the download will start automatically.
To install the Oracle Java package that you just downloaded, go to the location where you saved the .deb file. Double-click on the file to launch Ubuntu Software Center. Click on the Install button to start installing Oracle Java on your system.
Add Java to the Environment Variables
If you have installed Oracle Java from the official website, you will have to manually set the Java PATH variable on your system.
Find the directory where Java is installed on your local storage.
Issue the following command in your terminal. Replace path/to/java with the output of the previous command.
Verify if the JAVA_HOME path has been set up successfully. The Java directory will be displayed in the output.
Add the Java /bin directory to the PATH variable.
Ensure if the Java PATH is properly configured.
The output will display information related to the current version of Java on your system.
Related: Best Beginner Projects for New Programmers
Remove Java from Ubuntu
Once you're done with Java, you can easily uninstall it on Ubuntu. To remove OpenJDK and OpenJRE:
Launch the terminal using Ctrl + Alt + T.
Issue the following command to remove Java from your system.
sudo apt remove default-jdk default-jre
Confirm the uninstallation process by typing y.
OpenJDK and OpenJRE will be automatically removed from your computer.
For those who have installed the Oracle JDK, there are two ways to uninstall Java. You can double-click on the .deb package file to open Software Center and then click on the Remove button to uninstall the package.
Alternatively, you can remove the package using your system terminal.
Open up the terminal on Ubuntu.
Get the JDK package name using dpkg and grep.
dpkg --list | grep jdk
Uninstall the package using apt. Replace the package name with the output of the previous command.
sudo apt remove jdk-11.0.10
Type y and press Enter to continue the uninstall process.
Managing Java Installation on Ubuntu
While some Linux distributions like Arch Linux come with Java preinstalled, a majority of them don't. If you are using Ubuntu on your computer, chances are that you won't be able to run Java applications right away. You will have to install Java manually on your system.
Integrating a database with your Java application has become a lot easier now. JDBC (Java Database Connectivity) is a component of the Java SDK that allows you to add, delete, update, and store records containing information in a secure manner.