Monday, February 04, 2013

Multiple Java JDK(s), on your MacOSX environment

Note: if you are looking for an allaround guide to set up you environment, have a look here.

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 don't want to mess with paths, unless Apple drops support of java_home at some point:

First approach:

Second approach:

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.

Note 1: I still preserve the 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.

Note 2: There are lots of people asking how to force Eclipse, to use a very specific JDK version, especially when they have installed different JDK(s) on their Mac. Eventually I am using the eclipse.ini way (go to the,  right click,  Show Package Contents, Contents/MacOS/eclipse.ini).

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!


  1. My friend Paris,
    Regarding Eclipse also an idea is to create shell scripts to open your eclipse workspaces and there you can set a vm per workspace with the -vm argument and if i remember well the -data flag is for opening a specific workspace.

    The alias sounds a good idea but since in a normal development we never use the java or javac commands directly i usually set the java_home directly to the startup shell files of any tools I use maven, ant, flyway, app servers etc.

    1. 1. When it comes to eclipse- i just want to make sure the actual ide uses a more recent VM- when it comes to the project settings they can be manipulated either way.

      2. I try not to set in each tool the paths - rather than use the JAVA_HOME env variable. once referenced - can be used all over the place - that's why I really like to load different JAVA_HOMES on the same terminal

  2. You're the man thanks for this searched everywhere and used option 1 above :-)

  3. Thanks, exactly what I needed. My understanding is that the first option is preferred.

    1. Thanks . The first option is the one I use,indeed.

  4. Wish I could kiss you in the most platonic way!

  5. This works great! Thanks for sharing!

  6. Thanks for sharing such informative article on Java technology. It is most preferred technology among developers to create stunning mobile application fast and easy. It also created massive career opportunity for aspiring professionals.JAVA Training Institutes in Chennai

  7. Thanks for your informative post. Placement training is excellent option for students willing to enter software development industry with lucrative salary package.

  8. Thank you for sharing this information. This is good if we need to execute and use different JDK versions.

    For simple compilation however, we can use javac -target and define the target JVM version.

  9. Thanks for sharing this valuable information to our vision. You have posted a trust worthy blog keep sharing.
    html5 Training in chennai | html5 Training chennai

  10. Nice article i was really impressed by seeing this article, it was very interesting and it is very useful for Learners..
    Web designing Training in chennai | Web designing Training chennai

  11. The information you have given here are most worthy for me. I have implemented in my training program as well, thanks for sharing.

    Hadoop Training Chennai
    Hadoop Training in Chennai

  12. A very informative post. Helped a lot, as I used it as a reference to train my students. Thanks a bunch.
    HTML5 Course in T Nagar | HTML5 Course in T Nagar

  13. The information you have deliver here is really useful to make my knowledge good. Thanks for your heavenly post. It is truly supportive for us and I have accumulated some essential data from this blog.
    Informatica training institute in Chennai|Best Informatica Training Institute In Chennai|Informatica training in chennai

  14. Thanks for a wonderful share. Your article has proved your hard work and experience you have got in this field. Brilliant .i love it reading.
    ADG Consulting

  15. This information is impressive; I am inspired with your post writing style & how continuously you describe this topic. After reading your post, thanks for taking the time to discuss this, I feel happy about it and I love learning more about this topic..
    Informatica Training in chennai | QTP Training in Chennai

  16. This comment has been removed by the author.

  17. Nice article, Downloaded the latest Java6 from the below URL to get it install Ei Capitan,

    Thanks for the post.