DEV Community

Cover image for Manage Multiple JDK Versions on Your Computer
Pradipta Sarma
Pradipta Sarma

Posted on

6 1

Manage Multiple JDK Versions on Your Computer

Originally published on my blog

Java now released a new version every 6 months, the latest as on the date of this post is the JDK 15, while the LTS are released at longer intervals.

You might be using a version of Java for work or your personal use, and may need to use another version of it for a different project. Or, if you're one who would want to try out the newest version as soon as it comes out and also have the stable version installed, this can help. Although, you can pick up a JAR from anytime in the past 25 years, and most likely it will run on the newest version of Java, it is good to have an LTS (or whatever your main version of need is) installed for your actual projects.

To change a version of Java, you need to change the JAVA_HOME environment variable. And it's a pain to do it every time.

I have two projects that I work on almost everyday. One runs on Java 8 while the other is on Java 11. Here's what I did to be able to switch between them when needed.

export JAVA8_HOME=$(/usr/libexec/java_home -v1.8)
export JAVA11_HOME=$(/usr/libexec/java_home -v11)

alias java8='export JAVA_HOME=$JAVA8_HOME'
alias java11='export JAVA_HOME=$JAVA11_HOME'

# To use Java 8
java8

# To use Java 11
java11
Enter fullscreen mode Exit fullscreen mode

After doing so, I now have two aliases java8 and java11, calling which, sets my JAVA_HOME to either JAVA8_HOME or JAVA11_HOME. Adding a new one is as easy as exporting a new home and creating an alias for it.

In case you're using zsh, you can write these into your .zshrc file using echo or pasting them into the file manually and doing a source ~/.zshrc. An awesome tutorial to set up ZSH is up here or here

There is another way of doing this using jenv. A post on that will be here soon.

Image of Docusign

🛠️ Bring your solution into Docusign. Reach over 1.6M customers.

Docusign is now extensible. Overcome challenges with disconnected products and inaccessible data by bringing your solutions into Docusign and publishing to 1.6M customers in the App Center.

Learn more

Top comments (1)

Collapse
 
pradipta profile image
Pradipta Sarma

Looks interesting. Checking it out. Thanks.

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more

👋 Kindness is contagious

Discover a treasure trove of wisdom within this insightful piece, highly respected in the nurturing DEV Community enviroment. Developers, whether novice or expert, are encouraged to participate and add to our shared knowledge basin.

A simple "thank you" can illuminate someone's day. Express your appreciation in the comments section!

On DEV, sharing ideas smoothens our journey and strengthens our community ties. Learn something useful? Offering a quick thanks to the author is deeply appreciated.

Okay