In the dynamic world of Java development, you often need to manage multiple JDKs in Linux simultaneously. Indeed, this is crucial for:
- Ensuring Compatibility: Testing your applications across different JDK versions.
- Supporting Legacy Projects: Maintaining projects that rely on specific, older JDKs.
- Facilitating Multi-Project Development: Handling projects with diverse JDK requirements.
Therefore, this guide will walk you through the process of installing multiple JDKs and seamlessly managing the default JDK in your Linux environment.

1. Installing Multiple JDKs: Preparing Your Environment
To begin, you need to install the desired JDK distributions.
- Download and Extract: Download the JDK distributions (e.g., OpenJDK, Oracle JDK) from their official websites. Subsequently, extract the downloaded archives to a designated directory, such as
/usr/lib/jvm
. You can find the latest OpenJDK releases on the Official Oracle JDK website or OpenJDK website. - Example: Bash
sudo mkdir -p /usr/lib/jvm sudo tar -xzf jdk-11.0.15_linux-x64_bin.tar.gz -C /usr/lib/jvm/ sudo tar -xzf jdk-17.0.7_linux-x64_bin.tar.gz -C /usr/lib/jvm/
2. Setting the Default JDK: Using update-alternatives
Following the installation, you’ll use the update-alternatives
command to manage symbolic links and set the default JDK.
- Create Symbolic Links: Bash
sudo update-alternatives --install "/usr/bin/java" "java" "/usr/lib/jvm/jdk-11.0.15/bin/java" 1 sudo update-alternatives --install "/usr/bin/java" "java" "/usr/lib/jvm/jdk-17.0.7/bin/java" 2
In this case, the priority numbers (1 and 2) determine the order of preference. A lower number signifies higher priority. - Configure the Default JDK: Bash
sudo update-alternatives --config java
This command will display a list of available alternatives. Select the desired JDK version using the arrow keys and press Enter. - Verify the Default JDK: Bash
java -version
This command should display the version of the currently selected default JDK.
3. Managing JDK Alternatives: Advanced Techniques
Moreover, you can use these commands to manage your JDK alternatives.
- List Available Alternatives: Bash
update-alternatives --list java
- Display Current Default: Bash
update-alternatives --display java
- Change Default JDK: Bash
sudo update-alternatives --config java
- Remove an Alternative: Bash
sudo update-alternatives --remove java /usr/lib/jvm/jdk-11.0.15/bin/java
4. Setting Default JDK for Specific Users: Customization
Furthermore, if you need to set different default JDKs for individual users, create symbolic links within the user’s bin
directory.
- Example: Bash
ln -s /usr/lib/jvm/jdk-11.0.15/bin/java ~/bin/java
5. Managing javac
and Other Java Tools: Consistency
Similarly, repeat the update-alternatives
process for other Java tools like javac
, jar
, javadoc
, etc. This ensures consistency across your Java development environment. In addition, if you want to automate this process, you may find this article on shell scripting in linux helpful.
Important Notes: Best Practices
- Compatibility: Ensure that the JDK installations are compatible with your system architecture (e.g., x86_64).
- Updates: Regularly update your JDKs to benefit from the latest security patches and performance improvements.
- Java Version Managers: Consider using a Java Version Manager (like SDKMAN!) for more advanced and flexible JDK management.
Conclusion: Streamlining Your Java Development Workflow
In summary, by following these steps, you can effectively manage multiple JDKs in Linux and seamlessly switch between them. Thus, this flexibility is crucial for developers working on projects with diverse JDK requirements. Therefore, streamline your Java development workflow today!