Skip to content

Alternatives

When several packages provide the same command — two JDKs, two Python interpreters, several editors — the generic name on PATH is a symlink managed by the alternatives system rather than a real binary. /usr/bin/java points into /etc/alternatives, which points at the implementation currently selected.

On the RHEL family the command is alternatives; on Debian and Ubuntu it is update-alternatives. Both take the same arguments.

List what is registered under a name and which is selected:

Terminal window
alternatives --display java

Choose interactively from the installed versions:

Terminal window
sudo /usr/sbin/alternatives --config java

A JDK registers more than one command. Setting java changes what runs, but leaves javac, jar and the rest pointing wherever they were — switch each one, or select the grouped java family entry if the packaging provides it. Confirm the result:

Terminal window
java -version
javac -version

This changes the default for every user on the host. For a per-project toolchain, set JAVA_HOME in the build instead — a Gradle or Maven toolchain declaration pins the version in the repository, where it is visible and reproducible, rather than in the state of one machine.