Saturday, January 4, 2025
Google search engine
HomeGuest BlogsHow to set default Java version on Ubuntu / Debian

How to set default Java version on Ubuntu / Debian

.tdi_3.td-a-rec{text-align:center}.tdi_3 .td-element-style{z-index:-1}.tdi_3.td-a-rec-img{text-align:left}.tdi_3.td-a-rec-img img{margin:0 auto 0 0}@media(max-width:767px){.tdi_3.td-a-rec-img{text-align:center}}

In this guide, I’ll show you how you can set the default Java version on Ubuntu / Debian Linux system. It is common to run more than one version of Java in your Ubuntu or Debian system – For development reasons or varying applications requirements.

set default java ubuntu debian

Earlier on we had done an article on installing Java on Ubuntu / Debian:

Suppose you install Java 11 and you had another version of Java installed earlier, you can select default Java version to use using the update-alternatives --config java command.

.tdi_2.td-a-rec{text-align:center}.tdi_2 .td-element-style{z-index:-1}.tdi_2.td-a-rec-img{text-align:left}.tdi_2.td-a-rec-img img{margin:0 auto 0 0}@media(max-width:767px){.tdi_2.td-a-rec-img{text-align:center}}

Step 1: Checking Java versions installed on Ubuntu / Debian

To get a list of installed Java versions, run the command:

$ sudo update-java-alternatives --list
java-1.11.0-openjdk-amd64      1111       /usr/lib/jvm/java-1.11.0-openjdk-amd64
java-1.8.0-openjdk-amd64       1081       /usr/lib/jvm/java-1.8.0-openjdk-amd64

You’ll get a list of all Java editions that are installed on your Debian / Ubuntu system. Identify the version you wan to change to then proceed to next step.

Step 2: Set default Java version on Ubuntu / Debian

Once you have a list of Java version, set a default one by running the command. I’ll change mine from Java 11 to Java 8:

$ 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/java-8-openjdk-amd64/jre/bin/java   1081      manual mode

Press <enter> to keep the current choice[*], or type selection number: 2
update-alternatives: using /usr/lib/jvm/java-8-openjdk-amd64/jre/bin/java to provide /usr/bin/java (java) in manual mode

Check Java version

$ java -version
openjdk version "1.8.0_302"
OpenJDK Runtime Environment (build 1.8.0_302-8u302-b08-0ubuntu2-b08)
OpenJDK 64-Bit Server VM (build 25.302-b08, mixed mode)

The same can be done for javac.

$ 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/java-8-openjdk-amd64/bin/javac    1081      manual mode

Press <enter> to keep the current choice[*], or type selection number: 2
update-alternatives: using /usr/lib/jvm/java-8-openjdk-amd64/bin/javac to provide /usr/bin/javac (javac) in manual mode

If JAVA_HOME is not set correctly, run the command below to set from current default Java configured:

export JAVA_HOME=$(readlink -f /usr/bin/java | sed "s:bin/java::")

For JRE, use:

export JAVA_HOME=$(readlink -f /usr/bin/java | sed "s:jre/bin/java::")

Persistence can be achieved by placing the export command in your .bashrc or /etc/profilefile.

$ vim ~/.bashrc
export JAVA_HOME=$(readlink -f /usr/bin/java | sed "s:bin/java::")

We hope this article helped you to set the default Java version on Ubuntu / Debian Linux system. Stay connected for more articles on Java and Development tools.

Recommended books:

.tdi_4.td-a-rec{text-align:center}.tdi_4 .td-element-style{z-index:-1}.tdi_4.td-a-rec-img{text-align:left}.tdi_4.td-a-rec-img img{margin:0 auto 0 0}@media(max-width:767px){.tdi_4.td-a-rec-img{text-align:center}}

RELATED ARTICLES

Most Popular

Recent Comments