2

From https://stackoverflow.com/a/48374564/156458

update-alternatives --config java works by installing symlinks to commands as /usr/bin/java, /usr/bin/javac and so on.

How does update-alternatives --config somecommand find the symlinks for somecommand?

For example, does it rely on $PATH, and therefore work in the same way as how bash find the files for somecommand when running somecommand directly in bash?

If the file for somecommand is not a symlink, will update-alternative --config somecommand work?

Thanks.

Tim
  • 101,790

1 Answers1

4

The way update-alternatives works is described in its manpage. The list of available alternatives for a given command (or file, in general — alternatives aren’t limited to commands) is stored in a file in /var/lib/dpkg/alternatives on Debian and derivatives; thus the alternatives for java are stored in /var/lib/dpkg/alternatives/java. On Fedora, RHEL, and derivatives, the files are stored in /var/lib/alternatives.

When a package wishes to provide an alternative, it installs it using update-alternatives --install (and appropriate parameters); when it wishes to remove an alternative, it does so using update-alternatives --remove. You can use these to provide your own alternatives if necessary.

Note that in Java’s case specifically, on Debian and derivatives, the alternatives handling is a little more complex and you should use update-java-alternatives instead of manipulating all the alternatives manually.

If for some reason an alternative-managed file is not a symlink, update-alternatives will consider that the alternative is broken and will refuse to touch it.

Stephen Kitt
  • 434,908
  • Interesting note about java alternatives there. I'm not at all familiar with how or why that would be the case, however using update-alternatives has always worked for me when dealing with java. Additionally, while the original work was created by debian developers, RedHat-based distros have adapted it as well. Although, there is no specific update-java-alternatives command on RHEL, e.g. It might be worth noting as well, on RedHat-based systems, the path is /var/lib/alternatives for the alternative scripts. – ILMostro_7 Jan 22 '18 at 13:48
  • 1
    @ILMostro_7 right, update-alternatives still works, update-java-alternatives makes it easier to update all the JRE/JDK-provided tools when changing JVMs (look at the /var/lib/jvm/.*.jinfo files for details). Thanks for the comment, I’ve updated my answer. – Stephen Kitt Jan 22 '18 at 13:51
  • Thanks. Why "in Java’s case specifically, on Debian and derivatives, the alternatives handling is a little more complex and you should use update-java-alternatives instead of manipulating all the alternatives manually" ? I am reading https://askubuntu.com/questions/159575/how-do-i-make-java-default-to-a-manually-installed-jre-jdk, but still can't figure it out. – Tim Jan 22 '18 at 15:28
  • @Tim, for Java packages, there are lots of different alternatives, none of which are connected (partly because you might only have a sub-selection of the commands installed); so if you want to switch from Java 8 to Java 9 for example, you’d have to update a couple of dozen alternatives manually... update-java-alternatives does that for you. – Stephen Kitt Jan 22 '18 at 15:30
  • What are " a couple of dozen alternatives" that I have to update manually? Is $JAVA_HOME included? – Tim Jan 22 '18 at 15:39
  • @Tim see my comment addressed to ILMostro_7 above for details. $JAVA_HOME isn’t a file, so it isn’t included (it can’t be managed using alternatives). – Stephen Kitt Jan 22 '18 at 15:44
  • Thanks. https://askubuntu.com/a/521154/1471 seems to just use update_alternatives instead of update_java_alternatives, and I wonder if the way there is recommended? – Tim Jan 22 '18 at 16:02
  • @Tim, that answer explains how to install the Oracle JDK, and uses update-alternatives in the manual setup which doesn’t support update-java-alternatives. I wouldn’t recommend that approach. – Stephen Kitt Jan 22 '18 at 16:18
  • Thanks. Could you describe or point me to the approach that you recommend? Let me know if I should post a new question for it. – Tim Jan 22 '18 at 18:21
  • @Tim FWIW, RedHat adds the lines of text from those *.jinfo files into the /var/lib/alternatives/* files. So, maybe that would explain the lack of update-java-alternatives explicitly, as it's not needed because update-alternatives (as implemented on RHEL) has that already baked in by design. – ILMostro_7 Jan 22 '18 at 18:44
  • @Tim the approach I recommend on Debian & co. is as described in my answer. Feel free to ask another more specific question if you need more details, it’s somewhat beyond the scope of this question IMO! – Stephen Kitt Jan 22 '18 at 19:29
  • Thanks. I created a post https://unix.stackexchange.com/questions/418985/ways-to-configure-alternative-installations-of-oracle-jdk-on-ubuntu – Tim Jan 23 '18 at 01:57