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.
There are two potential ways - the first one is based on the java_home command line utility of Apple - the second one is based on manual path entries - that might be error prone or difficult to manage. So use the first one if you dont want to mess with paths, unless Apple drops support of java_home at some point:
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
If I want to switch on jdk6 or jdk8 I just make use of the alias commands (just type the name of the alias (e.g papo/> java6) - and the variable is being updated - accordingly.
symbolic link as, configured by the system. Meaning, if you do a 'which java' on your command line, you will get a ' /usr/bin/java ' sym link. Which is already loaded in your $PATH. The sym link as shown points to '/System/Library/Frameworks/JavaVM.framework/Versions/Current/Commands/java' . Some, may argue that you could update this specific configuration but, I don't find it problematic.
Add a line like the following - before the -vmargs section.
In my case I force eclipse to use the latest version of JDK7. (the bold part may change depending on your installation), use java_home to resolve the latest or have a look on the path below to find the verion you have installed.
Hope that helps! Happy coding!