How to install and configure Java on Ubuntu 22.04 Linux

In this article, we will see how to install and configure Java on Ubuntu 22.04 Linux step by step.

Java is one of the most popular programming languages and the JVM (Java’s virtual machine) is the run-time environment to run Java applications.

Verify java is installed or not on in Linux by running command.

java -verison

Command 'java' not found, but can be installed with: sudo apt install openjdk-11-jre-headless # version 11.0.17+8-1ubuntu2~22.04, or sudo apt install default-jre # version 2:1.11-72build2 sudo apt install openjdk-17-jre-headless # version 17.0.5+8-2ubuntu1~22.04 sudo apt install openjdk-18-jre-headless # version 18.0.2+9-2~22.04 sudo apt install openjdk-19-jre-headless # version 19.0.1+10-1ubuntu1~22.04 sudo apt install openjdk-8-jre-headless # version 8u352-ga-1~22.04

So Java is not installed.

Install and configure Java on Ubuntu 22.04 Linux

1. Update the software packages.

sudo apt-get update

2. Install default JRE on Ubuntu:

sudo apt install default-jre

Type ‘Y’ and hit enter.

3. To compile and run some specific Java programs in addition to the JRE, you may need the Java Development Kit (JDK).

sudo apt install default-jdk

Set the JAVA_HOME Environment Variable in Ubuntu 22.04 LTS

1. Find the java installed location using below command.

readlink -f /usr/bin/java

How to install and configure Java on Ubuntu 22.04 Linux1

2. Now, open /etc/environment file.

sudo nano /etc/environment

Add the following line at the end of the file, make sure to replace the location of your Java installation path.

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

3. Save the file and reload the file to apply the changes to your current session:

source /etc/environment

4. Verify that the environment variable is set or not.

echo $JAVA_HOME

Create and run sample java program on Ubuntu 22.04 Linux.

1. Create a file using nano and paste below program in the file and save it.

class r2schools{ public static void main(String args[]){ System.out.println("Welcome to r2schools"); } }

2. Compile the java file.

javac r2schools1.java

3. Run the java program.

java r2schools

Watch below video for the same steps:

Leave a Reply

Your email address will not be published. Required fields are marked *