The update-alternatives --config java
command is used in Linux to manage and switch between different installed versions of Java on your system. This command is part of the update-alternatives
system, which maintains symbolic links to set default versions of commands, libraries, and more.
Steps to Use update-alternatives --config java
:
- Open a Terminal: Launch your terminal application.
- Run the Command: Use the following command:
sudo update-alternatives --config java
- View Installed Java Versions: When you run the above command, it lists all the installed Java versions on your system along with their priority and path. It will look something like this:
There are 3 choices for the alternative java (providing /usr/bin/java). Selection Path Priority Status ------------------------------------------------------------ * 1 /usr/lib/jvm/java-11-openjdk-amd64/bin/java 1100 auto mode 2 /usr/lib/jvm/java-8-openjdk-amd64/bin/java 1080 manual mode 3 /usr/lib/jvm/java-17-openjdk-amd64/bin/java 1700 manual mode Press <enter> to keep the current choice[*], or type selection number:
- Select the Desired Version:
- Type the number corresponding to the version you want to use.
- Press Enter.
For example, to select Java 17, type
3
and press Enter. - Verify the Default Version: After switching, you can confirm the active Java version using:
java -version
Explanation of Output Fields:
- Selection: The number you enter to select a version.
- Path: The location of the Java binary.
- Priority: Determines the default version if in
auto mode
. - Status:
auto mode
: The system automatically selects the highest-priority version.manual mode
: A version was manually selected.
Example:
Current Setup:
sudo update-alternatives --config java
Output:
There are 2 choices for the alternative java (providing /usr/bin/java).
Selection Path Priority Status
------------------------------------------------------------
* 1 /usr/lib/jvm/java-8-openjdk-amd64/bin/java 1080 auto mode
2 /usr/lib/jvm/java-11-openjdk-amd64/bin/java 1100 manual mode
Press <enter> to keep the current choice[*], or type selection number:
- If you want to switch to Java 11, type
2
and press Enter.
Notes:
- Run as Root: You need
sudo
privileges to useupdate-alternatives
. - Custom Versions: If you’ve manually installed a Java version not listed, you can add it to the alternatives system using:
sudo update-alternatives --install /usr/bin/java java /path/to/java 1000
- This command can also manage other Java-related commands like
javac
andjar
.