Swap Java Versions on Linux: Commands and Options
In this article
To swap Java versions on Linux, select the installed Java runtime or development kit with your distribution’s alternatives tool, usually update-alternatives or alternatives. Then verify both java and, if you compile code, javac; changing the runtime alone may leave the compiler on a different version.
What a Java version switch changes
Linux systems can have several Java installations at once. A command such as java is usually reached through a symbolic link managed by the operating system’s alternatives system. Selecting an alternative changes which installed executable that link points to; it does not uninstall other versions.
Java has two commonly confused components:
- JRE or runtime: runs Java applications. Many current JDK distributions include a runtime, though packaging varies.
- JDK: includes development tools such as
javac, in addition to the runtime. You need it to compile Java source code.
The version shown by java -version is the version used when a shell runs java through its current PATH. It does not necessarily tell you which Java an IDE, service, container, or build tool uses.
Switch versions with the system alternatives tool
First list the Java installations registered with your system. The command differs by distribution:
- Debian and Ubuntu:
sudo update-alternatives --config java - Fedora, RHEL, Rocky Linux, and similar systems:
sudo alternatives --config java
The tool displays numbered choices and their paths. Enter the number for the desired installation. You may need administrator privileges because these links are system-wide. Check the result with:
java -version
If you also use the compiler, select it separately where alternatives are configured for it:
sudo update-alternatives --config javac
On systems using the other command, use sudo alternatives --config javac. Confirm with javac -version. Some distributions register additional Java tools, such as jar or javadoc, as separate alternatives. If consistency matters, inspect and select those too.
Check which installations are available
An alternatives menu only lists versions installed and registered with that system. To inspect the executable your shell finds, run:
which javareadlink -f "$(which java)"
The resolved path often reveals the selected installation. You can also ask a package manager what is installed. For example, Debian-based systems commonly use dpkg -l with a Java package-name filter, while Fedora-family systems use rpm -qa. Package names differ across vendors and distributions, so treat these as ways to inspect rather than universal installation commands.
If the version you want is absent, install a suitable JDK or runtime first. Choose a package from your distribution or a trusted JDK vendor, and check its support policy and compatibility with your application. Do not assume that installing a new JDK automatically makes it the active alternative.
Set JAVA_HOME when a program needs it
JAVA_HOME is an environment variable that points to a Java installation directory. Some build tools and applications require it, even when the java command works. It is separate from the alternatives selection: changing one does not reliably change the other.
Find the selected executable’s real path, then identify the JDK root above its bin/java location. For example, if the resolved path is /usr/lib/jvm/example-21/bin/java, the home directory is typically /usr/lib/jvm/example-21. Set it for the current shell with:
export JAVA_HOME=/usr/lib/jvm/example-21
export PATH="$JAVA_HOME/bin:$PATH"
Replace the example with the actual path on your machine. To make the setting persist, put appropriate export lines in your shell’s startup file, such as ~/.bashrc for many interactive Bash sessions, and start a new shell or reload the file. System services do not necessarily read your personal shell configuration; configure a service’s environment separately when required.
System-wide switching versus per-project versions
Alternatives are convenient when you want one default Java version for the machine. Because the selection is generally system-wide, changing it may affect other users or applications. A server process might also keep running with the Java version it started with until it is restarted.
For development, a project-specific version manager or build-tool configuration can be more suitable. Tools such as SDKMAN! manage user-level Java installations and let developers select versions without changing the system default. Build tools can also specify a required Java release for compilation, but that does not always control the JVM used to launch the build itself. Check both the build’s toolchain settings and the Java executable used to start it.
- Use system alternatives for a straightforward machine-wide default.
- Use a user-level manager when different projects need different JDKs.
- Configure services explicitly when they must run with a particular Java installation.
Common problems and checks
If java -version still shows the old version, check whether another executable appears earlier in PATH. Shell aliases, manually installed JDKs, and user-level version managers can take precedence over the alternatives link. Compare which java with the path shown by readlink -f, and open a fresh shell if you changed startup files.
If java and javac report different versions, select both alternatives and check JAVA_HOME. If a service or IDE still reports another version, inspect its configured runtime rather than assuming it inherits your terminal’s settings. Finally, an application’s compatibility is not determined only by the version number: vendor builds, required Java features, and application support policies can matter.
Frequently Asked Questions
How do I switch Java versions on Ubuntu?
Run sudo update-alternatives --config java, choose an installed version, and verify it with java -version. If you compile code, also configure javac.
How do I switch Java versions on Fedora?
Use sudo alternatives --config java to choose the registered runtime. Check the compiler separately with sudo alternatives --config javac, then verify both commands.
Does changing alternatives update JAVA_HOME?
Not necessarily. JAVA_HOME is an environment variable, so inspect and set it separately if your application or build tool requires it.
Can I keep multiple Java versions installed?
Yes. Alternatives are designed to select among registered installations; switching the default does not normally remove the others.
Why does my application use a different Java version?
It may use a hard-coded Java path, its own runtime setting, a service-specific environment, or a version manager. Check the application’s launch configuration and the process environment.