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 Linux distributions do not have Java preinstalled and users have to install it manually on their system.

Here’s 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.

  1. JDK (Java Development Kit)
  2. JRE (Java Runtime Environment)
  3. 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 apps. JVM acts as a virtual machine that creates an environment for running Java on your computer.

jdk and jre difference

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 Minecraft, you’ll need Java Runtime Environment. If your end goal is to learn Java and develop software, then you should install Java Development Kit.

Java Editions and Implementations

Now that you’ve decided what you want to download, it is time to get practical and start installing Java on Ubuntu.

Java has several editions that are available to download.

  1. Java Standard Edition (Java SE)
  2. Java Micro Edition (Java ME)
  3. Java Enterprise Edition (Java EE)
  4. JavaFX

For this article, we will be downloading the standard edition of Java, i.e. Java SE.

Like the different Java platforms, there are several Java implementations you can install on your system. Ubuntu repositories provide an open-source Java implementation known as OpenJDK and OpenJRE. 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 17, this guide will focus on the same. You are free to download other versions of Java if you want.

Install OpenJDK and OpenJRE on Ubuntu

You can install OpenJDK on Ubuntu using the terminal. You’ll have to use APT for this process, which is the default package manager that comes preinstalled in Debian-based Linux distributions.

  1. Launch the terminal by pressing Ctrl + Alt + T and update your system’s package list with:
     sudo apt update 
  2. Issue the following command to download OpenJDK:
     sudo apt install default-jdk 
  3. Type y and press Enter to confirm the installation.

Similarly, you can install OpenJRE on your system with:

 sudo apt install default-jre 

Verify if Java was successfully installed 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 on Ubuntu

The Ubuntu repositories do not provide the Oracle Java package. You can download the Oracle Java package from the official website, however.

Download: Oracle Java

Follow the steps below to install Oracle Java on Ubuntu:

  1. Head over to the Oracle Java Download webpage.
  2. Switch to the Java 17 (LTS) download section by clicking on the corresponding option.
    download oracle java 17 from the official website

  3. Click on the URL next to the x64 Debian Package option to download the package.
    download oracle java 17 DEB package

The website might 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 you just downloaded, go to the location where you saved the DEB file. Double-click on the file to launch Snap Store. If that doesn’t work, right-click the file and click Open With Other Application > Software Install.

When the Snap Store window pops up, click the Install button to start installing Oracle Java on your system.

install jdk17 with ubuntu software

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.

  1. Issue the below-given command in your terminal. Make sure to enter the Java path exactly as it appears on your system.
     export JAVA_HOME=/usr/lib/jvm/jdk-17 
  2. Verify if the path was set up successfully by printing the value of JAVA_HOME using echo. The Java directory will be displayed in the output.
     echo $JAVA_HOME 
  3. Add the Java /bin directory to the PATH variable with:
     export PATH=$PATH:$JAVA_HOME/bin 
  4. Ensure that the Java PATH is properly configured by running:
     java --version 

The output will display information related to the current version of Java.

How to 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 and 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 Oracle JDK, there are two ways to uninstall Java. You can double-click the DEB package file to open Snap Store and then click the Remove button to uninstall the package.

Alternatively, you can remove the package using your system terminal:

  1. Get the JDK package name using dpkg and grep.
     dpkg --list | grep jdk 
  2. Then, uninstall the package using APT. Remember to replace the package name with the output of the previous command.
     sudo apt remove jdk-17 
  3. Type y and hit Enter to uninstall the package.

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 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 securely.


Source link

Leave a Reply

%d bloggers like this: