In this article, I’ll walk you through the installation of Java JDK (Java Development Kit) on an Ubuntu Linux system. Java is a popular programming language used for development of Desktop Applications, Games, Mobile applications and many others. As a Java Developer, you’ll want to install the default Java available on Ubuntu upstream repositories instead of worrying about complex compiling from source hassle.
Installation of Java on Ubuntu
We will focus on the installation of the default version of Java on Ubuntu. You can check available versions with the command:
sudo apt update
Once the APT index is updated, check versions of Java available on the default repositories of Ubuntu:
$ apt policy default-jdk
default-jdk:
Installed: (none)
Candidate: 2:1.21-75+exp1
Version table:
2:1.21-75+exp1 500
500 http://nova.clouds.archive.ubuntu.com/ubuntu noble/main amd64 Packages
We will install both Java Runtime Environment (JRE) and the Java Development Kit (JDK):
sudo apt install default-jdk default-jre
A number of dependencies will be installed, you can validate and agree to install Java on Ubuntu with the y key:
...
The following NEW packages will be installed:
adwaita-icon-theme alsa-topology-conf alsa-ucm-conf at-spi2-common at-spi2-core ca-certificates-java
dconf-gsettings-backend dconf-service default-jdk default-jdk-headless default-jre default-jre-headless fontconfig
fonts-dejavu-extra gsettings-desktop-schemas gtk-update-icon-cache hicolor-icon-theme humanity-icon-theme
java-common libasound2-data libasound2t64 libatk-bridge2.0-0t64 libatk-wrapper-java libatk-wrapper-java-jni
libatk1.0-0t64 libatspi2.0-0t64 libavahi-client3 libavahi-common-data libavahi-common3 libcairo-gobject2 libcairo2
libcolord2 libcups2t64 libdatrie1 libdconf1 libdrm-amdgpu1 libdrm-intel1 libdrm-nouveau2 libdrm-radeon1 libepoxy0
libgdk-pixbuf-2.0-0 libgdk-pixbuf2.0-bin libgdk-pixbuf2.0-common libgif7 libgl1 libgl1-amber-dri libgl1-mesa-dri
libglapi-mesa libglvnd0 libglx-mesa0 libglx0 libgraphite2-3 libgtk-3-0t64 libgtk-3-bin libgtk-3-common
libharfbuzz0b libice-dev libice6 liblcms2-2 libllvm17t64 libpango-1.0-0 libpangocairo-1.0-0 libpangoft2-1.0-0
libpciaccess0 libpcsclite1 libpixman-1-0 libpthread-stubs0-dev librsvg2-2 librsvg2-common libsm-dev libsm6
libthai-data libthai0 libvulkan1 libwayland-client0 libwayland-cursor0 libwayland-egl1 libx11-dev libx11-xcb1
libxau-dev libxaw7 libxcb-dri2-0 libxcb-dri3-0 libxcb-glx0 libxcb-present0 libxcb-randr0 libxcb-render0
libxcb-shape0 libxcb-shm0 libxcb-sync1 libxcb-xfixes0 libxcb1-dev libxcomposite1 libxcursor1 libxdamage1
libxdmcp-dev libxfixes3 libxft2 libxi6 libxinerama1 libxkbfile1 libxmu6 libxrandr2 libxrender1 libxshmfence1
libxt-dev libxt6t64 libxtst6 libxv1 libxxf86dga1 libxxf86vm1 mesa-vulkan-drivers openjdk-21-jdk
openjdk-21-jdk-headless openjdk-21-jre openjdk-21-jre-headless session-migration ubuntu-mono x11-common x11-utils
x11proto-dev xorg-sgml-doctools xtrans-dev
0 upgraded, 123 newly installed, 0 to remove and 143 not upgraded.
Need to get 202 MB of archives.
After this operation, 627 MB of additional disk space will be used.
Do you want to continue? [Y/n] y
Confirm Java installation on Ubuntu
After successfully installing Java on Ubuntu, confirm the version with the java command line.
$ java -version
openjdk version "21.0.5" 2024-10-15
OpenJDK Runtime Environment (build 21.0.5+11-Ubuntu-1ubuntu124.04)
OpenJDK 64-Bit Server VM (build 21.0.5+11-Ubuntu-1ubuntu124.04, mixed mode, sharing)
Write a test Java application.
cat > HelloWorld.java <<HELLO
public class HelloWorld {
public static void main(String[] args) {
System.out.println("Hello World!");
}
}
HELLO
Run the test java application created.
$ java HelloWorld.java
Hello World!
If you have more that one version of Java installed, set default Java to use with the command:
$ sudo update-alternatives --config java
There is 1 choice for the alternative java (providing /usr/bin/java).
Selection Path Priority Status
------------------------------------------------------------
* 0 /usr/lib/jvm/java-21-openjdk-amd64/bin/java 2111 auto mode
1 /usr/lib/jvm/java-21-openjdk-amd64/bin/java 2111 manual mode
Press <enter> to keep the current choice[*], or type selection number:
You must have noted you can directly run Java source files in Java 11. The compilation step is automatically handled for you.
Recommended Java Books to read: