Install multiple JDKs on Mac OS X
It so happened that after watching JDK 8 in action in the GIDS Summit i was very much interested in trying out some of the features of JDK8, but i wasn’t sure of installing JDK 8 on my Mac because of the fact that the project i am working on is based on JDK 6 and will Mac allow me to uninstall JDK 8 and reinstall JDK 6 and in the process my OS shouldn’t be corrupted. So I took a small risk and just tried out.
So in this post we shall see how to install multiple JDKs on Mac OS X, and also switch between them easily as and when required.
1. Verify the existing JDK installation
The command to verify the existing Java version is java -version and which java is used to check the location of the java command.
Mac OS X 10.6 onward ship with JDK installed, here I have JDK 1.6 already installed.
2. Install JDK 7 & JDK 8
Now lets install JDK 7 and JDK 8, they can be downloaded from here as any other software installed on Mac.
3. Configure JDKs
Mac OS X requires us to set JAVA_HOME variable in ~/.bash_profile to /usr/libexec/java_home. On executing the command /usr/libexec/java_home -V we get the below output.
1 2 3 4 5 6 7 8 9 |
BANL11fe0f3d7:~ msampath$ /usr/libexec/java_home -V Matching Java Virtual Machines (4): 1.8.0_05, x86_64: "Java SE 8" /Library/Java/JavaVirtualMachines/jdk1.8.0_05.jdk/Contents/Home 1.7.0_55, x86_64: "Java SE 7" /Library/Java/JavaVirtualMachines/jdk1.7.0_55.jdk/Contents/Home 1.6.0_65-b14-462, x86_64: "Java SE 6" /System/Library/Java/JavaVirtualMachines/1.6.0.jdk/Contents/Home 1.6.0_65-b14-462, i386: "Java SE 6" /System/Library/Java/JavaVirtualMachines/1.6.0.jdk/Contents/Home /Library/Java/JavaVirtualMachines/jdk1.8.0_05.jdk/Contents/Home BANL11fe0f3d7:~ msampath$ |
The above output states that there are four versions of JDK installed on the Mac.
Now lets edit bash_profile and export JAVA_HOME as below.
1 2 3 4 5 |
vi ~/.bash_profile export JAVA_HOME=$(/usr/libexec/java_home) export JDK_HOME=$JAVA_HOME export JRE_HOME=$JAVA_HOME |
After changing the above and checking java -version we get the below, which means that by default Mac OS has selected the latest version on JDK on the OS.
1 2 3 4 5 6 |
BANL11fe0f3d7:~ source ~/.bash_profile BANL11fe0f3d7:~ msampath$ java -version java version "1.8.0_05" Java(TM) SE Runtime Environment (build 1.8.0_05-b13) Java HotSpot(TM) 64-Bit Server VM (build 25.5-b02, mixed mode) BANL11fe0f3d7:~ msampath$ |
Lets edit the bash_profile again in order to point to JDK 7
1 2 3 4 5 |
vi ~/.bash_profile export JAVA_HOME=$(/usr/libexec/java_home -v 1.7) export JDK_HOME=$JAVA_HOME export JRE_HOME=$JAVA_HOME |
build compile bash_profile and run java -version again
1 2 3 4 5 6 |
BANL11fe0f3d7:bin msampath$ source ~/.bash_profile BANL11fe0f3d7:bin msampath$ java -version java version "1.7.0_55" Java(TM) SE Runtime Environment (build 1.7.0_55-b13) Java HotSpot(TM) 64-Bit Server VM (build 24.55-b03, mixed mode) BANL11fe0f3d7:bin msampath$ |
Now we have successfully switched to JDK 7, similarly for switching to JDK 6 we should say 1.6 instead and repeat the above steps.
I hope this has been useful for you and I’d like to thank you for reading. If you like this article, please leave a helpful comment and share it with your friends.
Ji.. very useful info at right time, i was looking for this…
Welcome Sundar! Thanks for following my blog, and nice to know you have joined a startup instead of Giant.
ji… my terminal shows correct Java version and even my eclipse able to pick up jdk 1.7 version , but my IntelliJ doesn’t pickup the latest JDK (1.7) any idea on this… or have you faced this problem.?
(/System/Library/Frameworks/JavaVM.framework/Versions/CurrentJDK/Home) this is the location i am looking at for 1.7 jdk from IntelliJ…
Hi Sundar,
JVMVersion
1.6*
Edit the below file in Mac
/Applications/{Intelli_Home}/Contents/Info.plist, look out for the below key and change it appropriately.
No ji, that doesn’t help me, is my path correct in the intelliJ to select the latest JDK…? or should i be selecting /System/Library/Frameworks/JavaVM.framework/Versions/Current/…?
Got the solution Ji, found that my Jdk 1.7 installed on the following location (/Library/Java/JavaVirtualMachines/jdk1.7.0_55.jdk/Contents/Home), intelliJ doesn’t show up its intelligence to pickup the mentioned version of jdk by default ;)….
you got the solution great Sundar!!