How to Change JDK Version From the Command Line

Posted by: Bruce Snyder on 06/04/2012
Today I was trying to figure out how to quickly change the JAVA_HOME environment variable from the terminal using the /usr/libexec/java_home command-line utility. I looked at /usr/libexec/java_home --help but didn't pay close attention to the usages because I was in on the phone. As my friend Martijn Dashorts pointed out, there's a manpage for this utility. I am embarrassed to admit that I didn't even think to check for a manpage! My idiot moment for the day, I guess. Here are the two aliases I created:

alias setjdk16='export JAVA_HOME=$(/usr/libexec/java_home -v 1.6*)'
alias setjdk17='export JAVA_HOME=$(/usr/libexec/java_home -v 1.7*)'
Put these aliases in your ~/.bash_profile or ~/.profile, reload the profile and you're ready to go. BTW, if you haven't switched from iTerm to iTerm2 yet, you should do it now ;-).

About Bruce Snyder

Bruce Snyder

Bruce Snyder is a veteran of enterprise software development and a recognized leader in open source software. With over a decade of experience, Bruce has worked with a wide range of technologies including Java EE, Enterprise Messaging and Service Oriented Integration. In addition to his role as a senior software engineer at SpringSource, Bruce is also an Apache Member, a co-founder of Apache Geronimo and a developer for Apache ActiveMQ, Apache Camel and Apache ServiceMix. He is the co-author of Professional Apache Geronimo, Beginning Spring Framework 2 both from Wrox Press and is currently co-authoring ActiveMQ In Action for Manning Publications. Bruce also serves as a member of various JCP expert groups and is a recognized international speaker at industry conferences. Bruce lives in beautiful Boulder, Colorado with his family.

More About Bruce »

NFJS, the Magazine

May Issue Now Available
  • On the road to learning

    by Raju Gandhi
  • Refactoring to Modularity

    by Kirk Knoernschild
  • RESTful Groovy

    by Kenneth Kousen
  • Getting Started with D3.js

    by Brian Sletten
Learn More »