Table of Contents
Condition
Updating Package Cache: Keeping Your System Up-to-Date
sudo apt update
Installing the JRE/JDK using Apt
sudo apt install default-jre
java -version
openjdk version "11.0.17" 2022-10-18
OpenJDK Runtime Environment (build 11.0.17+8-post-Ubuntu-1ubuntu222.04)
OpenJDK 64-Bit Server VM (build 11.0.17+8-post-Ubuntu-1ubuntu222.04, mixed mode, sharing)
sudo apt install default-jdk
javac -version
javac 11.0.17
Installing Oracle JDK
sudo apt install -y libc6-x32 libc6-i386
wget https://download.oracle.com/java/19/latest/jdk-19_linux-x64_bin.deb
wget https://download.oracle.com/
java/19/latest/jdk-19_linux-x64_bin.deb
sudo apt install ./jdk-19_linux-x64_bin.deb
sudo update-alternatives --install /usr/bin/java java /usr/lib/jvm/jdk-19/bin/java 100
sudo update-alternatives --install /usr/bin/javac javac /usr/lib/jvm/jdk-19/bin/javac 100
Manage Java
sudo update-alternatives --config java
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/jdk-19/bin/java 100 manual mode
Press
sudo update-alternatives --config javac
There are 2 choices for the alternative javac (providing /usr/bin/javac).
Selection Path Priority Status
------------------------------------------------------------
* 0 /usr/lib/jvm/java-11-openjdk-amd64/bin/javac 1111 auto mode
1 /usr/lib/jvm/java-11-openjdk-amd64/bin/javac 1111 manual mode
2 /usr/lib/jvm/jdk-19/bin/javac 100 manual mode
Again, choose 2 to use the OracleJDK compiler as your default compiler and press Enter.
java -version
java version "19.0.1" 2022-10-18
Java(TM) SE Runtime Environment (build 19.0.1+10-21)
Java HotSpot(TM) 64-Bit Server VM (build 19.0.1+10-21, mixed mode, sharing)
javac -version
javac 19.0.1
Configuring the Java Home Directory Environment Variable
sudo update-alternatives --config java
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/jdk-19/bin/java 100 manual mode
sudo nano /etc/environment
JAVA_HOME="/usr/lib/jvm/java-11-openjdk-amd64"
Save and close /etc/environment.
Reload /ect/environment:
source /etc/environment
Now check out the value of your Java Home variable:
echo $JAVA_HOME
This should print the path you’ve set:
/usr/lib/jvm/java-11-openjdk-amd64
You have learned how to install Java on your Ubuntu system using two different methods. The first method involves installing the OpenJDK version available in your Ubuntu repositories, while the second method involves installing the official OracleJDK. Additionally, you have become proficient in installing and switching between both versions effortlessly, making it easier to meet your changing needs.