Yet again, a small tip towards configuring your Java development environment on a Mac (OSX 10.8.x+). If you are really starting right now, I recommend you read one of my previous posts, a quick and clean (I suppose) way to set up your environment variables and start your Java coding. My tip for today is about easily switching from one JDK version to another on your command line. Currently at work I am forced to compile towards JDK6, at the same time I want to use as much as possible JDK7 on my pet projects and ‘force’ my tools (namely IDE(s)) to use the related jvm.
Last but not least, since we are getting closer on the JDK8 release, it is really interesting to experiment a bit on the new features, through the available early snapshot releases, provided by Oracle.
I have modified my ~/.profile adding the following lines. Of course versions and paths on certain JDK(s) depend on the releases you have installed, by downloading the related dmg(s) and running the setup package/installer.
#export CUSTOM HOME(S) variables #last JDK 6 by Apple export JAVA_6_HOME=/System/Library/Frameworks/JavaVM.framework/Home #latest JDK 7 by Oracle export JAVA_7_HOME=/Library/Java/JavaVirtualMachines/jdk1.7.0_13.jdk/Contents/Home #latest preview JDK 8 by Oracle export JAVA_8_HOME=/Library/Java/JavaVirtualMachines/jdk1.8.0.jdk/Contents/Home #default JDK is 7 export JAVA_HOME=$JAVA_7_HOME
What I really did is to store and export 3 different environment variables,one fore each flavor of the JDK I currently have installed. Then I select the one to be the default (that is JDK7)- see inline comment. At the same time, I am actually creating 3 different alias commands to update the value of the JAVA _HOME variable depending on the jdk I wish to have ‘loaded’ in my current shell. So when I open my terminal my default java-vm setting is set to JDK7