Configuring multiple Java runtimes in parallel on the Mac

Configuring multiple Java runtimes in parallel on the Mac

I am new to Mac OS so I'm documenting some of my findings as a developer. I often need multiple, different JDK versions based on what I am doing. For example, I recently worked with the prophet machine learning library in Python that spins up Java in the background but that's only compatible with JDK up to version 17 where the latest version is 23 right now.

I like using the Adopium Temurin JDK for a few reasons:

1. Free and Open Source

  • Temurin is part of the Eclipse Adoptium project, which focuses on producing high-quality, open-source builds of the JDK.
  • It’s free to use with no licensing fees, which is a significant advantage over proprietary JDKs like Oracle’s.

2. High-Quality Builds

  • The builds are rigorously tested to ensure compatibility with Java standards and provide a reliable runtime for Java applications.
  • It adheres to the TCK (Technology Compatibility Kit) to ensure compliance with Java SE specifications.

3. Active Community Support

  • The Adoptium project has an active community of developers and contributors, providing regular updates and security patches.
  • The community-driven approach ensures rapid response to bugs and vulnerabilities.

4. Regular Updates

  • Temurin provides timely updates for security, performance, and bug fixes.
  • The release cadence aligns with OpenJDK, ensuring you’re always up to date.

5. Cross-Platform Availability

  • It supports multiple platforms, including Windows, macOS, Linux, and containerized environments.
  • This makes it suitable for a wide range of development and production use cases.

6. Performance

  • Temurin is optimized for performance and stability, making it a robust choice for both development and production environments.
  • It’s tested against real-world workloads to ensure it meets industry standards.

7. Wide Adoption and Ecosystem Compatibility

  • Temurin is widely adopted by organizations and integrates seamlessly into modern build pipelines and cloud ecosystems.
  • It is compatible with popular build tools (like Maven and Gradle) and frameworks (like Spring Boot).

8. Governance by the Eclipse Foundation

  • The Adoptium project is governed by the Eclipse Foundation, a reputable organization known for its transparency and focus on community-driven open-source software.

9. Great for Containers

  • Temurin provides Docker images optimized for Java, which are ideal for deploying applications in cloud-native environments.

10. Choice of LTS and Latest Versions

  • You can choose between Long-Term Support (LTS) versions for stability and newer versions for access to the latest features.

So to install multiple JDKs, I used homebrew:

brew install temurin
brew install temurin@17

The nice thing is that brew install temurin adds / updates to the latest version of the JDK and brew install temurin@17 keeps the latest version of the JDK 17.

Now, all I have to do to activate one or the other is set the proper JAVA_HOME environment variable to point to the version of the JDK I want to use. I did this using the .zshrc file but it can also be done with a bash script. I just comment the one I don't want to use.

# Export JAVA home
export JAVA_HOME=$(/usr/libexec/java_home -v 17)
#export JAVA_HOME=$(/usr/libexec/java_home -v 23)

You can always find out the active version of the JDK by running:

java --version

which will output as follows:

openjdk 17.0.13 2024-10-15
OpenJDK Runtime Environment Temurin-17.0.13+11 (build 17.0.13+11)
OpenJDK 64-Bit Server VM Temurin-17.0.13+11 (build 17.0.13+11, mixed mode, sharing)

From this command line, you can then start VS Code or any other IDE with the proper command.

code .