Q: How to switch Java version on Linux when multiple versions are installed using the alternatives command?
I have two Java versions installed on Linux. How can I switch between them using the alternatives command?
I have two Java versions installed on Linux. How can I switch between them using the alternatives command?
That’s a good solution. But if we don’t want both we can just install the new one and remove old one.
Switching Java versions on Linux, especially when multiple versions are installed, can be straightforward if you use the alternatives command. Many developers and admins face this scenario because different applications often require distinct Java versions. Fortunately, managing multiple Java versions has become effortless due to Linux’s robust alternatives system.
First, you’ll want to list all installed Java versions managed by alternatives. Run the following command in your terminal:
$ sudo alternatives –config java
Once this command is executed, a list of all available Java executables appears. You will immediately notice that each installed Java version is assigned a selection number.
Next, after reviewing the list, you should type the number corresponding to the Java version you wish to activate as the system default, then press Enter. The switch is applied almost instantly, and all subsequent Java commands will utilize the newly selected version.
Additionally, in order to check which Java version is now active on your Linux system, simply run:
$ sudo java -version
This action will confirm that the switch has occurred and assures you that the correct Java version is in effect.
Recent Blog