Switching between multiple Java versions on OS X quickly

If you’re a Java developer like me, you will need to work on various Java versions. If so, adding the following in your ~./bash_profile makes switching between versions a snap. (As of this writing, I’m running on OS X El Capitan.)


alias java6='export JAVA_HOME=$(/usr/libexec/java_home -v 1.6);java -version'
alias java7='export JAVA_HOME=$(/usr/libexec/java_home -v 1.7);java -version'
alias java8='export JAVA_HOME=$(/usr/libexec/java_home -v 1.8);java -version'
alias java9='export JAVA_HOME=$(/usr/libexec/java_home -v 9);java -version'

NOTE: Java 9 above is the beta version, so it may be 1.9 once it’s been finalized.

Leave a Reply

Your email address will not be published. Required fields are marked *