Flash Sale:75% Off Hosting + Free DomainEnds in13h47m14sView Plans
Hostvento logoHostvento

Install OpenJDK on Ubuntu

Install the free Java Development Kit, pick the version and set JAVA_HOME.

VPS and Cloud Servers1 min read5 screenshots

Why OpenJDK

  • To develop Java applications you need the JDK (the full Java Development Kit).
  • Many enterprise and server applications run on Java.
  • OpenJDK is the free and open-source alternative to Oracle's Java.

The packages:

  • JRE (openjdk-17-jre) lets you run Java programs.
  • JDK (openjdk-17-jdk) lets you also compile and develop.
  • default-jdk installs the recommended JDK of your Ubuntu version.

Check if Java is installed

java -version
Checking the Java version

Install OpenJDK

Update the package list:

sudo apt update
Updating the package list

Install the default JDK:

sudo apt install default-jdk
Installing the default JDK

Or choose a version:

sudo apt install openjdk-17-jdk

Find the packages that you can install:

sudo apt-cache search openjdk
Searching for OpenJDK packages

Check the install

java --version
javac --version
The Java version

Set JAVA_HOME

Find the path:

readlink -f $(which javac) | sed "s:/bin/javac::"

Open your shell file:

nano ~/.bashrc

Add (use the path you found):

export JAVA_HOME=/usr/lib/jvm/java-17-openjdk-amd64
export PATH=$PATH:$JAVA_HOME/bin

Load the change:

source ~/.bashrc

For all users, put the lines in /etc/environment or in a file in /etc/profile.d/.

Switch between versions

If several versions are installed:

sudo update-alternatives --config java

Remove OpenJDK

sudo apt remove openjdk-17-jdk openjdk-17-jre

Quick recap

  • sudo apt install default-jdk is the easy way.
  • Check with java --version.
  • Set JAVA_HOME in ~/.bashrc.