Assuming one has installed a JDK in /opt/java/jdk1.8.0_144 then:

  1. Install the alternative for javac

    $ sudo update-alternatives --install /usr/bin/javac javac /opt/java/jdk1.8.0_144/bin/javac 1
    
  2. Check / update the alternatives config:

    $ sudo update-alternatives --config javac
    

If there is only a single alternative for javac you will get a message saying so, otherwise select the option for the new JDK.

To check everything is setup correctly then:

$ which javac
/usr/bin/javac

$ ls -l /usr/bin/javac
lrwxrwxrwx 1 root root 23 Sep  4 17:10 /usr/bin/javac -> /etc/alternatives/javac

$ ls -l /etc/alternatives/javac
lrwxrwxrwx 1 root root 32 Sep  4 17:10 /etc/alternatives/javac -> /opt/java/jdk1.8.0_144/bin/javac

And finally

$ javac -version
javac 1.8.0_144

Repeat for java, keytool, jar, etc as needed.

Answer from Richard Grimshaw on Stack Overflow
Top answer
1 of 12
84

Assuming one has installed a JDK in /opt/java/jdk1.8.0_144 then:

  1. Install the alternative for javac

    $ sudo update-alternatives --install /usr/bin/javac javac /opt/java/jdk1.8.0_144/bin/javac 1
    
  2. Check / update the alternatives config:

    $ sudo update-alternatives --config javac
    

If there is only a single alternative for javac you will get a message saying so, otherwise select the option for the new JDK.

To check everything is setup correctly then:

$ which javac
/usr/bin/javac

$ ls -l /usr/bin/javac
lrwxrwxrwx 1 root root 23 Sep  4 17:10 /usr/bin/javac -> /etc/alternatives/javac

$ ls -l /etc/alternatives/javac
lrwxrwxrwx 1 root root 32 Sep  4 17:10 /etc/alternatives/javac -> /opt/java/jdk1.8.0_144/bin/javac

And finally

$ javac -version
javac 1.8.0_144

Repeat for java, keytool, jar, etc as needed.

2 of 12
76

You will notice a big change when selecting options if you type in "java -version" after doing so. So if you run update-alternatives --config java and select option 3, you will be using the Sun implementation.
Also, with regards to auto vs manual mode, making a selection should take it out of auto mode per this page stating:

When using the --config option, alternatives will list all of the choices for the link group of which given name is the master link. You will then be prompted for which of the choices to use for the link group. Once you make a change, the link group will no longer be in auto mode. You will need to use the --auto option in order to return to the automatic state.

And I believe auto mode is set when you install the first/only JRE/JDK.

Top answer
1 of 4
213
sudo update-alternatives --config java

Configures the default for the program "java". That's the Java VM.

sudo update-alternatives --config javac

Configures the default Java compiler.

You can also see that, because the first command lists a lot of "JRE" (Java Runtime Environment) folders and the Program is just called "java".

If I check which version is being used by issuing the command java -version or javac -version, I can see, that each command changes the program being used.

However, using update-java-alternatives with a JDK Version changes both programs for me. Using the first commands, you can use a Java VM and Java Compiler from different JDKs.

update-java-alternatives requires presence of a file with extension .jinfo in directory /usr/lib/jvm. The openjdk package is shipped with a .jinfo file, the jdk of Oracle (formerly Sun) is not. As alternative, you configure alternatives without update-java-alternatives:

For example, to add java from jvm-directory /usr/lib/jvm/jdk-12.0.1 (default directory of Debian package of Oracle) with priority 2082, use the following command:

sudo update-alternatives --install /usr/bin/java java /usr/lib/jvm/jdk-12.0.1/bin/java 2082

As for switching for different development environments:

Are you talking about starting the IDE itself with different Java versions or using different versions in the IDE for compilation and running your app?

  • For 1.: You can specify which JVM to use in the eclipse.ini, as described here. I don't know how to do that for the Arduino IDE.

  • For 2.: In Eclipse you can select the JRE/JDK to be used in Window -> Preferences -> Java -> Installed JREs. And under Java -> Compiler you could choose an older Java compliance if you wish.

EDIT: This DigitalOcean page also has a very nice explanation of everything related to Java on Ubuntu.

2 of 4
36

update-java-alternatives is a program to update alternatives for jre/jdk installations.

update-alternatives is a symbolic link management system for linux (I'm sure there is little news here).

You can, and really should, use both update-java-alternatives and update-alternatives together.

Firstly, be sure to have the all the alternatives configured correctly. java and javac are but a few. There is javadoc, rmic, serialver and others, substituting the above variables for: native2ascii and /opt/jdk1.8.0_40/bin/native2ascii should report if the alternative is installed and/or selected.

When all the alternatives are configured you can then create links in /usr/lib/jvm to your manual instalation.

In order to configure update-java-alternatives you must use a hidden file with the same name as your directory but prefixed by a . (dot).

Hope this helps.

Bibliography

man -S 8 update-java-alternatives

http://tech.lanesnotes.com/2008/03/using-alternatives-in-linux-to-use.html

https://stackoverflow.com/questions/6477415/how-to-set-oracles-java-as-the-default-java-in-ubuntu

Discussions

unix - Default JAVA, $JAVA_HOME vs sudo update-alternatives --config java - Stack Overflow
I am on Ubuntu and I have set the following in my ~/.bashrc file: export JAVA_HOME=/opt/jdk1.8.0_91 export PATH=$JAVA_HOME/bin:$PATH and then: echo $JAVA_HOME >/opt/jdk1.8.0_91 java -version ... More on stackoverflow.com
🌐 stackoverflow.com
version - What is the meaning of update-alternatives --config java command - Unix & Linux Stack Exchange
The + was my selection, what does * mean? [root ~]$ update-alternatives --config javac There are 2 programs which provide 'javac'. Selection Command ----------------------------------------... More on unix.stackexchange.com
🌐 unix.stackexchange.com
January 4, 2017
update-alternatives has two entries, one in auto and other in manual - Unix & Linux Stack Exchange
On my Ubuntu 15.04, sudo update-alternatives --config java shows: Selection Path Priority Status ------------------------------------------------... More on unix.stackexchange.com
🌐 unix.stackexchange.com
Debian Alternatives System - anything similar on Arch?
no, but there is one specifically for java , which makes me think that the idea isn’t as bad as other comments might suggest. it’s a problem also for e.g. python packages providing a binary: they can be installed once for python2 and once for python3. but which should contain the binary without version suffix? More on reddit.com
🌐 r/archlinux
20
17
November 29, 2014
🌐
Linux Mint Forums
forums.linuxmint.com › board index › main edition support › software & applications
typing: sudo update-alternatives --config java gives an erorr - Linux Mint Forums
November 5, 2017 - Run the command below to set the default Java: ... sudo update-alternatives --config java [/color] Type in a number to select a Java version.
🌐
SUSE
documentation.suse.com › sles › 15-SP5 › html › SLES-all › cha-update-alternative.html
update-alternatives: managing multiple versions of commands and files | Administration Guide | SLES 15 SP5
April 6, 2026 - To change the default java command to refer to a previous version, run: > sudo update-alternatives --config java root's password: There are 2 choices for the alternative java (providing /usr/bin/java).
🌐
DEV Community
dev.to › thegroo › install-and-manage-multiple-java-versions-on-linux-using-alternatives-5e93
Install and manage multiple Java versions on Linux using alternatives - DEV Community
February 10, 2022 - sudo update-alternatives --install /usr/bin/javac javac /usr/lib/jvm/azul-open-jdk-17/bin/javac 2 ... sudo update-alternatives --install /usr/bin/jshell jshell /usr/lib/jvm/azul-open-jdk-17/bin/jshell 2 · Now that you have the new JDK installed and configured you can easily switch between versions using:
Top answer
1 of 1
15

It only changes a symlink located (on most distro I guess) at /etc/alternatives/java. Absolutely NO change in the environment variable you set $JAVA_HOME is made.

First look at from where the command is found, you can do :

$which java
/usr/bin/java

The which command shows /usr/bin/java in my Debian distro. This file is a symlink which points to /etc/alternatives/java.

$ls -l /usr/bin | grep java
java -> /etc/alternatives/java

Then you follow the symlink :

$ls -l /etc/alternatives/java
/etc/alternatives/java -> /path/to/my/java/installation/1.x/bin/java

This shows that /etc/alternatives/java is another symlink. When you do an update-alternatives on java, you just change this symlink target to another one.

Then, why doesn't the executed version change when you do the update-alternatives command ? I guess it's because of the order the executables are found in $PATH. Since you added a directory to the PATH environment variable, there are now two possible java executables : one in /usr/bin and the other in /opt/jdk1.8.0_9, but only the first one found will be taken into account when you'll type java commands.

And because you set

PATH=$JAVA_HOME/bin:$PATH

The first one will be found in $JAVA_HOME/bin aka /opt/jdk1.8.0_91 . Because you made /opt/jdk1.8.0_9 appear before /usr/bin which is defined by default in the the PATH variable. You can check it by typing in a terminal

$echo $PATH
/usr/local/bin:/usr/bin:/bin:/usr/local/games:/usr/games:/path/to/my/java/installation/1.x/bin

You can see that my java/bin dir is located after the others defined in the PATH.

To correct this, you just have to concatenate $JAVA_HOME/bin after $PATH, like this :

PATH=$PATH:$JAVA_HOME/bin

This way you will be able to choose the default java executable from alternatives and the java exe found in $JAVA_HOME/bin will be discarded. But to be consistent, in most cases you should choose the same java exe as in $JAVA_HOME/bin.

🌐
Linux find Examples
queirozf.com › entries › update-alternatives-changing-java-version-on-ubuntu
Update Alternatives: Changing Java version on Ubuntu
May 2, 2022 - $ sudo update-alternatives --list java /usr/lib/jvm/java-11-openjdk-amd64/bin/java /usr/lib/jvm/java-8-openjdk-amd64/jre/bin/java · Use sudo update-alternatives --config java and choose the number you wish to use
Find elsewhere
🌐
Ming's Blog
bitmingw.com › 2019 › 08 › 28 › ubuntu-update-alternatives
Changing the Default Program with update alternatives | Ming's Blog
January 1, 2026 - To set java to use JDK 8 as the default, you can use an interactive command update-alternatives --config java.
🌐
Medium
medium.com › @ayeshajayasankha › how-to-install-and-switch-between-alternative-java-versions-66b3671fc382
How To Install And Switch Between Alternative Java Versions | by Ayesha Jayasankha | Medium
July 3, 2019 - When you run this in terminal it shows us what are the available versions that you can switch on.You can choose the number and after you confirm it, it will be your default java version. You can change your java compiler version with following command. sudo update-alternatives — config javac ·
🌐
Medium
irsyadsec.medium.com › java-not-found-in-update-alternatives-config-java-after-installing-java-on-linux-d88e499e5c4f
Java not found in “update-alternatives — config java” after installing java on linux | by Irsyad Muhammad Fawwaz | Medium
May 4, 2023 - Now that we have the path of Java ... Enter the following command in your terminal: sudo update-alternatives --install /usr/bin/java java /usr/lib/jvm/<jdk-folder>/bin/java 1...
🌐
GitHub
gist.github.com › bench › 3935f5e72c7dca83a4e31210b2c305e9
java and javac update-alternatives · GitHub
$ sudo update-alternatives --install /usr/bin/java java /home/bchenebault/Apps/jdk1.5.0_22/bin/java 1 $ sudo update-alternatives --install /usr/bin/java java /home/bchenebault/Apps/jdk1.6.0_45/bin/java 2 $ sudo update-alternatives --install /usr/bin/java java /home/bchenebault/Apps/jjdk1.7.0_79/bin/java 3 $ sudo update-alternatives --install /usr/bin/java java /home/bchenebault/Apps/jdk1.8.0_101/bin/java 4 · $ sudo update-alternatives --config java ·
🌐
Django CAS
djangocas.dev › blog › linux › switch-java-version-with-update-alternatives
Switch Java Version with update-alternatives - django-cas-ng
July 7, 2024 - # update-alternatives --list java /usr/lib/jvm/java-11-openjdk-arm64/bin/java /usr/lib/jvm/java-8-openjdk-arm64/jre/bin/java · Use update-alternatives --config java to update java version in interactive mode.
🌐
GitHub
gist.github.com › jeffcogswell › 8c47fb21355faf346a7336998ce76e07
Update Alternatives for Java · GitHub
cd /usr/lib/jvm sudo tar xf ~/Downloads/jdk-8u91-linux-x64.tar.gz sudo chown -R root:root jdk1.8.0_91 sudo update-alternatives --install /usr/bin/java java /usr/lib/jvm/jdk1.8.0_91/bin/java 1 sudo update-alternatives --install /usr/bin/javac javac /usr/lib/jvm/jdk1.8.0_91/bin/javac 1 sudo update-alternatives --install /usr/bin/jexec jexec /usr/lib/jvm/jdk1.8.0_91/lib/jexec 1 sudo update-alternatives --config java sudo update-alternatives --config javac sudo update-alternatives --config jexec
🌐
Williamdemeo
williamdemeo.github.io › linux › update-alternatives.html
update-alternatives - William DeMeo
March 13, 2014 - sudo update-alternatives --install "/usr/bin/java" "java" "/usr/lib/jvm/jdk1.7.0/bin/java" 1; sudo update-alternatives --install "/usr/bin/javac" "javac" "/usr/lib/jvm/jdk1.7.0/bin/javac" 1; sudo update-alternatives --install "/usr/bin/javaws" "javaws" "/usr/lib/jvm/jdk1.7.0/bin/javaws" 1; sudo update-alternatives --install "/usr/bin/jcontrol" "jcontrol" "/usr/lib/jvm/jdk1.7.0/bin/jcontrol" 1; sudo chmod a+x /usr/bin/java; sudo chmod a+x /usr/bin/javac; sudo chmod a+x /usr/bin/javaws; sudo chmod a+x /usr/bin/jcontrol; sudo chown -R root:root /usr/lib/jvm/jdk1.7.0; The following commands are interactive and should be invoked individually: sudo update-alternatives --config java sudo update-alternatives --config javac sudo update-alternatives --config javaws sudo update-alternatives --config jcontrol
🌐
Igalia
blogs.igalia.com › dpino › 2011 › 10 › 13 › configuring-different-jdks-with-alternatives
Configuring different JDKs with alternatives - Unweaving the Web
Now if we list the alternatives for ‘java‘. $ sudo alternatives --config java Selection Command ----------------------------------------------- *+ 1 /usr/java/jdk1.6.0_25/bin/java
🌐
Ubuntu
manpages.ubuntu.com › trusty › man(8)
Ubuntu Manpage: update-java-alternatives - update alternatives for jre/sdk installations
List all installed packages (or just <jname>) providing information to set a bunch of java alternatives. Verbose output shows each alternative provided by the packages. ... Switch all alternatives of registered jre/sdk installations to automatic mode. ... Set all alternatives of the registered ...