Struggling to Manage Multiple Java Versions? Here's How!

Published on

Struggling to Manage Multiple Java Versions? Here’s How!

Java is a powerful and versatile programming language, widely used in enterprise applications, mobile services, and web applications. However, managing multiple Java versions can become a headache for developers. Whether you are working on legacy systems or developing new applications, switching between Java versions often creates confusion and complications.

In this post, we’ll explore effective methods to manage multiple Java versions seamlessly, from installation to switching between them. We will leverage tools like SDKMAN and Docker, and highlight the importance of using a version manager for better productivity.

Why Manage Multiple Java Versions?

Before diving into the solutions, let’s discuss why you might need various Java versions:

  • Legacy Applications: Older applications may require specific Java versions that are no longer the default.
  • Research and Development: Working with new features in the latest Java release while ensuring compatibility with older versions.
  • Testing: Ensuring your applications work across different Java environments.

If you find yourself juggling multiple Java versions, it’s crucial to have a management strategy.

Setting Up SDKMAN

SDKMAN is an excellent tool that simplifies managing parallel versions of multiple Software Development Kits, including Java. With SDKMAN, you can easily install, switch, and configure different SDKs as per your project's needs.

Step 1: Install SDKMAN

To install SDKMAN, open a terminal and run the following command:

curl -s "https://get.sdkman.io" | bash

Once the installation script is finished, initialize SDKMAN:

source "$HOME/.sdkman/bin/sdkman-init.sh"

Step 2: Install Multiple Java Versions

After SDKMAN is installed, you can view available Java versions with:

sdk list java

This command will display all the Java versions you can install. You might see something like this:

================================================================================
Available Java Versions
================================================================================
     11.0.10-open
     14.0.2-open
     15.0.1-open
     16.0.1-open
     17.0.0-open
     .....
================================================================================

To install a specific version, use the command:

sdk install java 11.0.10-open

Step 3: Switching Java Versions

Once you’ve installed the desired Java versions, switching between them is straightforward. Use the following command:

sdk use java 14.0.2-open

This command sets your terminal to use Java 14.0.2 for the current session. To set a version globally, use:

sdk default java 11.0.10-open

This command sets the default Java version for all terminal sessions.

Step 4: Confirming Java Version

To confirm that the correct Java version is being used, run:

java -version

The output will show the currently active Java version. For example:

openjdk version "11.0.10" 2021-01-19
OpenJDK Runtime Environment (build 11.0.10+9)
OpenJDK 64-Bit Server VM (build 11.0.10+9, mixed mode)

Why SDKMAN?
SDKMAN simplifies version management. It offers the ability to easily switch between versions, makes updating Java versions simple, and allows you to manage different SDKs, all from the command line in one installation.

Using Docker for Java Version Management

Another effective strategy to manage multiple Java versions is using Docker containers. Docker encapsulates applications and their dependencies into containers, making it easy to run different versions of Java without conflict.

Step 1: Install Docker

If you have not installed Docker, download and install it from the official Docker website.

Step 2: Pulling a Java Image

You can easily start containers with specific Java versions. Here’s how to pull a Java image:

docker pull openjdk:11
docker pull openjdk:14

This command downloads OpenJDK images for versions 11 and 14.

Step 3: Running a Java Container

Run a container with a specific Java version using:

docker run -it openjdk:11 bash

This command initiates an interactive shell session within a container running OpenJDK 11. And if you want to use Java 14:

docker run -it openjdk:14 bash

Step 4: Verify Java Version in the Container

Inside the Docker container, verify the Java version:

java -version

You should see the version details similar to running it directly on your development environment. This way, you are encapsulated within a controlled environment that does not affect your local system.

Why Use Docker?
Using Docker isolates your Java applications and their dependencies. Each project can use its version of Java without conflicting with others. Furthermore, this setup is beneficial for continuous integration and deployment (CI/CD) processes.

Closing the Chapter

Managing multiple Java versions doesn't have to be a tedious process. Tools like SDKMAN and Docker drastically simplify the task, allowing developers to focus on what they do best: writing code.

By leveraging these tools, you can avoid conflicts and issues that arise from using incompatible Java versions in your workflows. Whether for legacy applications, testing, or staying updated with the newest features, you now have robust methods to manage your Java environment effectively.

Give SDKMAN or Docker a try! Managing your Java versions will soon become as routine as compiling your code.

Additional Resources

For further details, check out:

Happy coding!