How to Install Java on Ubuntu 22.04

How to Install Java on Ubuntu 22.04

Posted on

[*]

How one can Set up Java on Ubuntu 22.04. Java is without doubt one of the hottest programming languages which is extensively used to constructed purposes. It runs on all main working methods and gadgets.

On this information you’ll learn to set up a number of variations of Java (11, 17, 18) Runtime Atmosphere (JRE), Java Improvement Equipment (JDK) and Open JDK.

Additionally, you will learn to swap between Java variations and in addition the Java Improvement package variations.

Accessible Java variations in Ubuntu 22.04

Begin by updating the packages to the most recent model obtainable.

sudo apt replace
sudo apt improve

By default Ubuntu 22.04 repositories embrace two OpenJDK packages, Java Runtime Atmosphere (JRE) and Java Improvement Equipment (JDK).

To examine the obtainable packages you should utilize the next command.

sudo java -version
Output
Command 'java' not discovered, however could be put in with:
apt set up openjdk-11-jre-headless  # model 11.0.15+10-0ubuntu0.22.04.1, or
apt set up default-jre              # model 2:1.11-72build2
apt set up openjdk-17-jre-headless  # model 17.0.3+7-0ubuntu0.22.04.1
apt set up openjdk-18-jre-headless  # model 18~36ea-1
apt set up openjdk-8-jre-headless   # model 8u312-b07-0ubuntu1
Ask your administrator to put in one in all them.

In case you are undecided what to put in you may select the default OpenJDK (JDK 11) model.

Set up Java Runtime 11

Execute the next command to put in the default Java Runtime Atmosphere (JRE), which is able to set up the JRE from OpenJDK 11

 sudo apt set up default-jre

As soon as the set up is accomplished you may confirm the Java model utilizing the next command.

 java -version 

You’ll obtain an output just like the one beneath.

openjdk model "11.0.15" 2022-04-19
OpenJDK Runtime Atmosphere (construct 11.0.15+10-Ubuntu-0ubuntu0.22.04.1)
OpenJDK 64-Bit Server VM (construct 11.0.15+10-Ubuntu-0ubuntu0.22.04.1, combined mode, sharing)

Now you’ve put in Java 11 Runtime in your Ubuntu 22.04.

Set up Java Improvement Equipment

If want Java Improvement Equipment to compile Java packages, you can even set up Open JDK

 sudo apt set up default-jdk

As soon as the set up is accomplished you may confirm the Java model utilizing the next command.

 java -version 

You’ll obtain an output just like the one beneath.

javac 11.0.15

Now you’ve put in Java Improvement Equipment in your Ubuntu 22.04.

Set up Java JDK 17

Execute the next command to put in the default Java JDK, which may even set up the JRE from OpenJDK 17.

 sudo apt set up openjdk-17-jdk

As soon as the set up is accomplished you may confirm the Java model utilizing the next command.

 java -version 

You’ll obtain an output just like the one beneath.

openjdk model "17.0.3" 2022-04-19
OpenJDK Runtime Atmosphere (construct 17.0.3+7-Ubuntu-0ubuntu0.22.04.1)
OpenJDK 64-Bit Server VM (construct 17.0.3+7-Ubuntu-0ubuntu0.22.04.1, combined mode, sharing)

Now you’ve put in Java 17 Runtime in your Ubuntu 22.04.

Set up Java JDK 18

Execute the next command to put in the default Java JDK, which may even set up the JRE from OpenJDK 18.

 sudo apt set up openjdk-18-jdk

As soon as the set up is accomplished you may confirm the Java model utilizing the next command.

 java -version 

You’ll obtain an output just like the one beneath.

openjdk model "18-ea" 2022-03-22
OpenJDK Runtime Atmosphere (construct 18-ea+36-Ubuntu-1)
OpenJDK 64-Bit Server VM (construct 18-ea+36-Ubuntu-1, combined mode, sharing)

Now you’ve put in Java 18 Runtime in your Ubuntu 22.04.

Swap between Java (JRE) Variations

You probably have a number of Java variations put in, you may change the model simply utilizing the next command.

sudo update-alternatives --config java

This command prompts you to decide on Java model.

There are 3 decisions for the choice java (offering /usr/bin/java).

  Choice    Path                                         Precedence   Standing
------------------------------------------------------------
* 0            /usr/lib/jvm/java-18-openjdk-amd64/bin/java   1811      auto mode
  1            /usr/lib/jvm/java-11-openjdk-amd64/bin/java   1111      guide mode
  2            /usr/lib/jvm/java-17-openjdk-amd64/bin/java   1711      guide mode
  3            /usr/lib/jvm/java-18-openjdk-amd64/bin/java   1811      guide mode

Press <enter> to maintain the present alternative[*], or sort choice quantity:

Select the quantity and press Enter.

Swap between Java (JDK) Variations

You have to additionally change the Java growth package model, in any other case you would possibly get into compatibility points.

To vary the javac model you should utilize the beneath command.

sudo update-alternatives --config javac

This command prompts you to decide on Java Improvement model.

There are 3 decisions for the choice javac (offering /usr/bin/javac).

  Choice    Path                                          Precedence   Standing
------------------------------------------------------------
* 0            /usr/lib/jvm/java-18-openjdk-amd64/bin/javac   1811      auto mode
  1            /usr/lib/jvm/java-11-openjdk-amd64/bin/javac   1111      guide mode
  2            /usr/lib/jvm/java-17-openjdk-amd64/bin/javac   1711      guide mode
  3            /usr/lib/jvm/java-18-openjdk-amd64/bin/javac   1811      guide mode

Press <enter> to maintain the present alternative[*], or sort choice quantity:

Select the quantity and press Enter.

Now you’ve configured the Java Improvement Equipment model additionally.

Configure JAVA_HOME Atmosphere Variable

To setup path it’s worthwhile to discover the set up path utilizing the update-alternatives command.

sudo update-alternatives --config java 

Right here you will note the trail of Java.

  • OpenJDK 11 is positioned at /usr/lib/jvm/java-11-openjdk-amd64/bin/java
  • OpenJDK 17 is positioned at /usr/lib/jvm/java-17-openjdk-amd64/bin/java
  • OpenJDK 18 is positioned at /usr/lib/jvm/java-18-openjdk-amd64/bin/java

Now you may add this to the setting file.

 sudo nano /and so forth/setting

Now add the trail as proven beneath, right here proven the tactic so as to add Java 11 to the trail.

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

For the adjustments to be utilized both logout and login or execute the next command for the present session.

supply /and so forth/setting 

To confirm the setting variable of Java

echo $JAVA_HOME

You’ll get the set up path you simply set.

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

That’s all.

Take your first step in the direction of a career in Java software development with this introduction course

Conclusion

Now you’ve discovered tips on how to set up a number of variations of Java 11, 17 or 18 and configure JAVA_HOME on Ubuntu 22.04.

Thanks on your time. In case you face any downside or any suggestions, please depart a remark beneath.

offshore vps