Site icon DesignLinux

How to Install Java on Ubuntu 22.04

How to Install Java on Ubuntu 22.04

In this tutorial we will show how install Java on Ubuntu 22.04. Java is one of the most popular programming language used to build flexible and maintainable applications and systems.

Java comes in two different implementations OpenJDK and Oracle Java. Oracle java provides commercial features. It’s license allows only non-commercial use of software, for personal use or development use.

The default Ubuntu 22.04 repositories include two OpenJDK packages, Java Runtime Environment (JRE) and Java Development Kit (JDK).

Prerequisites

You should have a Ubuntu 22.04 instance with a system user with sudo privileges.

How to Install Java on Ubuntu 22.04

Perform the following steps to install Java on Ubuntu 22.04:

Step 1 – Update System

First of all you have to update your system using the below given command:

sudo apt update

Step 2 – Install OpenJDK

By default Ubuntu 22.04 includes Open JDK 11, which is an open-source variant of the JRE and JDK.

sudo apt install openjdk-jdk

Step 2 – Verify Installation

Once the installation is complete, you can verify it by checking the Java version:

java -version

It should show output as below:

openjdk version "11.0.17" 2022-11-18
OpenJDK Runtime Environment (build 11.0.17+8-post-Ubuntu-3ubuntu1)
OpenJDK 64-Bit Server VM (build 11.0.17+8-post-Ubuntu-3ubuntu1, mixed mode, sharing)

All done! Java is install on your Ubuntu 22.04 system.

Set the default Java version

You can set the default Java version, If you have multiple Java versions installed on your Ubuntu system. First of all, check the current default version by typing:

java -version

To change the default version use the update-alternatives command:

sudo update-alternatives --config java

It should show output like this:

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-19-openjdk-amd64/jre/bin/java   1191      manual mode

Press <enter> to keep the current choice[*], or type selection number:

It will show the list of installed Java versions. Select the version which you want to set as default by entering number and hit Enter key.

JAVA_HOME Environment Variable

The JAVA_HOME environment variable holds the Java installation location which used by the Java applications.

At first, use update-alternatives command to find where the Java versions are installed:

sudo update-alternatives --config java

It will show the path where Java is installed. In this example the installation paths are as follows:

Now, open the /etc/environment file by typing:

sudo nano /etc/environment

To set Java 11 as default, set Java 11 installation path to JAVA_HOME as given below, at the end of file:

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

Next, Save and exit the text editor. Finally, issue the source command as follows.

source /etc/environment

To confirm the Java environment variable setting, run the command.

echo JAVA_HOME

You should see the path to the Java installation:

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

Uninstall Java

Uninstall of Java is simple process like any other packages installed with apt:

For example, to uninstall the openjdk-jdk package simply run:

sudo apt remove openjdk-jdk

Conclusion

The latest LTS version OpenJDK 11 and previous LTS version OpenJDK 11 are available in the default Ubuntu 22.04 repositories. The installation is a very simple and straightforward task. You learned how to install different java version on Ubuntu 22.04.

If you have any query or suggestion, feel free to leave a comment.

Exit mobile version