Manage Multiple JDKs in Linux: Default & Switch Guide



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.

Manage Multiple JDKs in Linux: Default & Switch Guide

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: Bashsudo 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: Bashsudo 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: Bashsudo 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: Bashjava -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: Bashupdate-alternatives --list java
  • Display Current Default: Bashupdate-alternatives --display java
  • Change Default JDK: Bashsudo update-alternatives --config java
  • Remove an Alternative: Bashsudo 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: Bashln -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!

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.