What is the command update-alternatives used for?

Take this example:

sudo update-alternatives --install /usr/bin/java java /usr/local/java/jre1.7.0_09/bin/java 1

What does it do? How is it different from adding jdk to the path?

Let's say that I have run the command. How would I revert back to the original state?

  • 2
    You can also use sudo update-alternatives --config java and javac and javaws to choose between installed versions. – Shayan Feb 28 '18 at 15:04

It updates the links in /etc/alternatives to point to the program for this purpose. There's lots of examples, like x-www-browser, editor, etc. that will link to the browser or editor of your preference. Some scripts or system tools may want you to edit a file manually (e.g. configuration conflict in dpkg) and they'll look into the alternatives to give you the editor of choice. For java, this is the Java runtime environment - Oracle's, OpenJRE, etc.

The links in /etc/alternatives are just symbolic links. You can see them using for example

ls -l /etc/alternatives

Moreover, the regular /usr/bin binaries are also symlinks. E.g.:

ls -l /usr/bin/java
  lrwxrwxrwx 1 root root 22 Aug 14 10:33 /usr/bin/java -> /etc/alternatives/java
ls -l /etc/alternatives/java
  lrwxrwxrwx 1 root root 46 Aug 14 10:33 /etc/alternatives/java -> /usr/lib/jvm/java-6-openjdk-amd64/jre/bin/java

So, no PATH has to be modified. It just uses symbolic links.

  • So in other words I can type in a single command and it will prompt me for choices. That is the only added advantage over setting a path right. Also lets say I have a different vendor of java also installed How would I update java to point to both the two different vendors? – Developer Android Dec 27 '12 at 11:33
  • You can't point to both like you describe. The whole purpose of it is to have a single pointer to the implementation of the entry as in the example given. Without questions. And any of the candidates should work - it should just be a preference of the administrator. And again: it has nothing to do with PATHs. – gertvdijk Dec 27 '12 at 11:43
  • 1
    Okay. That clears things up. I know I am coming back to paths but I just want to understand the difference between them . In case of a path we have to explicitly specify the location but in this case it just sym links so that when we type java it searches /etc/alternatives first. Or am i wrong in making this assumption? – Developer Android Dec 27 '12 at 12:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.