About Paris Apostolopoulos

Paris is a senior software engineer focusing on J2EE development, loves Business process modelling and is keen on software quality challenges. He is passionate about Java and Java communities. He is a co-founder and administrator of the first Java User Group in greece(JHUG.gr) and occasional speaker on meet-ups and seminars and regular blogger.

Multiple Java JDK(s), on your MacOSX environment

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

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 Eclipse.app -> 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.

-vm
/Library/Java/JavaVirtualMachines/jdk1.7.0_13.jdk/Contents/Home/bin/

 

Reference: Multiple Java JDK(s), on your MacOSX environment from our JCG partner Paris Apostolopoulos at the Papo’s log blog.

Related Whitepaper:

Bulletproof Java Code: A Practical Strategy for Developing Functional, Reliable, and Secure Java Code

Use Java? If you do, you know that Java software can be used to drive application logic of Web services or Web applications. Perhaps you use it for desktop applications? Or, embedded devices? Whatever your use of Java code, functional errors are the enemy!

To combat this enemy, your team might already perform functional testing. Even so, you're taking significant risks if you have not yet implemented a comprehensive team-wide quality management strategy. Such a strategy alleviates reliability, security, and performance problems to ensure that your code is free of functionality errors.Read this article to learn about this simple four-step strategy that is proven to make Java code more reliable, more secure, and easier to maintain.

Get it Now!  

2 Responses to "Multiple Java JDK(s), on your MacOSX environment"

  1. There is also java_home.

    export JAVA_HOME=$(/usr/libexec/java_home -v 1.8)

  2. javapapo says:

    Yes true, unfortunately this post is not updated . I have updated the notes on my blog . Here is the link http://javapapo.blogspot.gr/2013/12/setup-your-java-development-environment.html

Leave a Reply


− two = 5



Java Code Geeks and all content copyright © 2010-2014, Exelixis Media Ltd | Terms of Use | Privacy Policy
All trademarks and registered trademarks appearing on Java Code Geeks are the property of their respective owners.
Java is a trademark or registered trademark of Oracle Corporation in the United States and other countries.
Java Code Geeks is not connected to Oracle Corporation and is not sponsored by Oracle Corporation.

Sign up for our Newsletter

20,709 insiders are already enjoying weekly updates and complimentary whitepapers! Join them now to gain exclusive access to the latest news in the Java world, as well as insights about Android, Scala, Groovy and other related technologies.

As an extra bonus, by joining you will get our brand new e-books, published by Java Code Geeks and their JCG partners for your reading pleasure! Enter your info and stay on top of things,

  • Fresh trends
  • Cases and examples
  • Research and insights
  • Two complimentary e-books