LinuxCommandLibrary

update-java-alternatives

Manage system Java installations

SYNOPSIS

update-java-alternatives [--jre-only] [--jdk-only] {--list | --set <name> | --auto | --query <name> | --help | --version}

PARAMETERS

--list
    Displays a list of all installed and registered Java environments managed by the alternatives system, along with their current status.

--set
    Selects and activates a specific Java environment (identified by <name>, e.g., java-11-openjdk-amd64) as the system's default for all Java commands.

--auto
    Configures Java alternatives automatically, typically selecting the highest-priority installed Java version to be the default.

--query
    Provides detailed information about a specific Java alternative identified by <name>, including its associated slave links.

--jre-only
    Restricts the operation to only Java Runtime Environment (JRE) related alternatives, such as the java command.

--jdk-only
    Restricts the operation to only Java Development Kit (JDK) related alternatives, such as the javac, jar, and javadoc commands.

--help
    Shows a summary of command usage and available options.

--version
    Displays the version information of the update-java-alternatives script.

DESCRIPTION

The update-java-alternatives command is a specialized utility on Debian and Ubuntu-based Linux distributions designed to manage multiple Java Development Kit (JDK) and Java Runtime Environment (JRE) installations. It acts as a wrapper around the more general update-alternatives system, specifically streamlining the configuration of Java-related binaries like java, javac, javadoc, and jar. When a new Java package is installed, this command (or the package itself) registers all its associated binaries with the alternatives system under a unified name (e.g., java-17-openjdk-amd64). This allows users and system administrators to easily switch the active Java version for all these tools simultaneously, ensuring consistency across the environment. It eliminates the need for manual symlink manipulation, simplifying the process of maintaining environments that require different Java versions or frequent switching between them. It's particularly invaluable for developers working on projects targeting various Java releases or for systems hosting applications with diverse Java dependencies.

CAVEATS

This command is primarily designed for Debian and Ubuntu-based Linux distributions and may not be available or function identically on other operating systems. Operations that modify system-wide alternatives (e.g., --set, --auto) typically require superuser privileges (i.e., running with sudo). It effectively manages Java installations provided through official package repositories; manually installed Java versions might not be automatically recognized unless they are manually registered with the update-alternatives system.

MECHANISM OF OPERATION

update-java-alternatives manages a group of related symbolic links that point to different Java executables. When you select a specific Java installation (e.g., via --set java-17-openjdk-amd64), the command identifies all 'slave' links associated with that master Java alternative. It then automatically updates each of these slave links (such as /usr/bin/java, /usr/bin/javac, /usr/bin/javadoc) to point to the corresponding executables within the chosen Java installation's directory. This ensures that all Java-related tools on your system consistently belong to the same active Java environment.

TYPICAL USAGE

Common usage involves first listing available Java installations with update-java-alternatives --list. Once the desired version's 'name' is identified (e.g., java-8-openjdk-amd64 or java-17-openjdk-amd64), the user can switch to it using sudo update-java-alternatives --set <name>. This simplifies development workflows where different projects might require different Java versions, or for ensuring a specific JRE is active for system-wide applications.

HISTORY

The update-java-alternatives utility is part of the java-common package on Debian/Ubuntu systems. Its development was driven by the need to simplify the management of multiple concurrent Java installations, a common scenario in development and server environments. It leverages the underlying update-alternatives framework, which was already established for managing symbolic links for various system commands. The command evolved to provide a user-friendly interface specifically tailored for Java, automating the complex task of linking numerous Java-related binaries (like java, javac, jar) to a single chosen Java environment, thereby ensuring consistency and ease of switching between versions.

SEE ALSO

update-alternatives(8), java(1), javac(1), dpkg(8), apt(8)

Copied to clipboard