04 June 2012

How to Change JDK Version From the Command Line

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 ;-).