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.

🌐
Igalia
blogs.igalia.com › dpino › 2011 › 10 › 13 › configuring-different-jdks-with-alternatives
Configuring different JDKs with alternatives - Unweaving the Web
$ alternatives --config java + 1 /usr/lib/jvm/jre-1.6.0-openjdk/bin/java * 2 /etc/alternatives/java_sdk/bin/java 3 /usr/lib/jvm/jre-1.5.0-gcj/bin/java 4 /usr/java/latest/bin/java 5 /usr/java/jdk1.7.0/bin/java
Discussions

linux - update-alternatives: warning: /etc/alternatives/java is dangling - Stack Overflow
I am facing the problem while running the java program from command line. I have openjdk-6-jdk installed in my linux system and still shows the error relates with the openjdk-7-jdk. Initially i ha... More on stackoverflow.com
🌐 stackoverflow.com
I deleted /etc/alternatives/java - Unix & Linux Stack Exchange
I'm on Ubuntu 12.04 on a 32-bits Dell machine. I was trying to install java JDK for 32-bits linux platform and I accidentally deleted the file /etc/alternatives/java. I would love to recreate my... More on unix.stackexchange.com
🌐 unix.stackexchange.com
November 11, 2013
java - What is the difference between JAVA_HOME and update-alternatives? - Unix & Linux Stack Exchange
What are the difference between JAVA_HOME and using alternatives? ... Alternatives is a tool that will manage the locations of the installed software using links under the control of the alternatives tool. These links are ultimately managed under /etc/alternatives with intermediate links created ... More on unix.stackexchange.com
🌐 unix.stackexchange.com
bash - setting JAVA_HOME and PATH with update-alternatives - Unix & Linux Stack Exchange
I didnt go for rest since my PATH is auto adjusted by alternatives. It works when my jdk selected is 17 but i dont see any reason why it shouldn't work with other jdk versions. ... Still not clear - Why we need dirname twice ? export JAVA_HOME=$(dirname readlink -f /etc/alternatives/java) this ... More on unix.stackexchange.com
🌐 unix.stackexchange.com
May 2, 2020
🌐
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 - Example 22.1: Alternatives System of the java command # /usr/bin/java 1 -> /etc/alternatives/java 2 -> /usr/lib64/jvm/jre-10-openjdk/bin/java 3
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

🌐
LinuxVox
linuxvox.com › blog › update-alternatives-warning-etc-alternatives-java-is-dangling
How to Fix update-alternatives: warning: /etc/alternatives/java is dangling on Linux After Removing OpenJDK 7 — linuxvox.com
This warning indicates a "dangling symbolic link"—a link that points to a file or directory that no longer exists. In this case, the system’s default java symlink (/etc/alternatives/java) was configured to point to OpenJDK 7, which you’ve since removed.
Find elsewhere
🌐
Django CAS
djangocas.dev › blog › linux › switch-java-version-with-update-alternatives
Switch Java Version with update-alternatives - django-cas-ng
July 7, 2024 - Instead, it is a symbolic link to a name in the alternatives directory, which in turn is a symbolic link to the actual file referenced. This is done so that the system administrator’s changes can be confined within the /usr/local/etc directory. In Debian/Ubunut, java is symbol link in /bin/java ...
Top answer
1 of 1
14

Alternatives

Alternatives is a tool that will manage the locations of the installed software using links under the control of the alternatives tool.

These links are ultimately managed under /etc/alternatives with intermediate links created under a directory in $PATH, typically /usr/bin.

Example

$ ls -l /usr/bin/java
lrwxrwxrwx. 1 root root 22 Feb 24 17:36 /usr/bin/java -> /etc/alternatives/java

$ ls -l /etc/alternatives/java
lrwxrwxrwx. 1 root root 73 Feb 24 17:36 /etc/alternatives/java -> /usr/lib/jvm/java-1.7.0-openjdk-1.7.0.60-2.4.5.0.fc19.x86_64/jre/bin/java

$JAVA_HOME

$JAVA_HOME is where software can be told to look through the use of an environment variable. Adding it to the $PATH simply adds the executables present in $JAVA_HOME/bin to your $PATH. This is sometimes necessary for certain applications.

The 2 mechanisms are related but can be used together or independent of each other, it really depends on the Java application which mechanism is preferable.

What I do

I typically use $JAVA_HOME for some GUI applications, but in general use it only for server installations that make use of Jetty, Tomcat, or JBOSS, for example.

For these installations I'll still use alternatives to manage the Java installations prior to setting the $JAVA_HOME. I like doing it this way in cases where I might need to have multiple installations of Java.

Alternatives does allow you to have certain tools use one installation of Java while other tools use a completely different one.

References

  • 5 Minute Guide to using the alternatives Command on Fedora/CentOS/RHEL
  • Using 'Alternatives' in Linux to use a different Java package
🌐
Red Hat
access.redhat.com › solutions › 6979452
/etc/alternatives/java changed from java-11-openjdk to java-1.8.0-openjdk after updates - Red Hat Customer Portal
On RHEL 8 systems with both java-11-openjdk and java-1.8.0-openjdk installed, where the alternatives java is set to java-11-openjdk path, the /etc/alternatives/java changed from java-11-openjdk to java-1.8.0-openjdk after applying java-11-openjdk updates.
🌐
Red Hat
access.redhat.com › solutions › 6232511
How to set default Java version with alternatives tool in RHEL on a per user basis - Red Hat Customer Portal
1 week ago - Selection Command ------------... /var/lib/alternatives/java.new: Permission denied Using the alternatives tool allows you to set the system default version as root but does not currently provide options to set different defaults for ...
🌐
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 - If you installed and configured an additional version in the guides, could have been super helpful because that's the whole point - "working with multiple versions of java", otherwise it's just another "How to install java in Linux - Tutorial" Still forced to just work with /etc/environment | $PATH update
🌐
FedoraForum.org
forums.fedoraforum.org › showthread.php
[SOLVED] java - multiple versions confusing
January 26, 2021 - 1 root root 32 Jan 26 15:17 jre-11-openjdk -> /etc/alternatives/jre_11_openjdk lrwxrwxrwx. 1 root root 39 Dec 22 06:24 jre-11-openjdk-11.0.9.11-9.fc33.x86_64 -> java-11-openjdk-11.0.9.11-9.fc33.x86_64 lrwxrwxrwx. 1 root root 29 Jan 26 15:17 jre-openjdk -> /etc/alternatives/jre_openjdk and " alternatives --list | grep java" should look like this:
🌐
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 - Now enter the following function to the etc/profile file. JAVA_HOME=<Directory where JAVA has been extracted>/jdk1.8.0PATH=$PATH:$HOME/bin:$JAVA_HOME/binexport JAVA_HOMEexport PATH · If this not working, enter the following function replacing upper one · export JAVA_HOME=<Directory where JAVA has been extracted>/jdk1.8.0export PATH=$PATH:$HOME/bin:$JAVA_HOME/bin ... sudo update-alternatives — install “/usr/bin/java” “java” “<Directory where JAVA has been extracted>/bin/java” 1sudo update-alternatives — install “/usr/bin/javac” “javac” “<Directory where JAVA has been extracted>/bin/javac” 1sudo update-alternatives — install “/usr/bin/javaws” “javaws” “<Directory where JAVA has been extracted>/bin/javaws” 1
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.

🌐
GitLab
0xdf.gitlab.io › 2020 › 03 › 24 › update-alternatives.html
update-alternatives | 0xdf hacks stuff - GitLab
March 24, 2020 - root@kali# tracelnk /bin/nc f: /bin/nc l bin -> usr/bin l nc -> /etc/alternatives/nc l nc -> /bin/nc.traditional l bin -> usr/bin · I’ll run it again and select option 0 to go back to auto for nc. The challenge that got me on this path involved a Java Jar file that was created with Java 8.