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.
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
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.
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.
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)
Lets edit the bash_profile again in order to point to JDK 7
export JAVA_HOME=$(/usr/libexec/java_home -v 1.7)
build compile bash_profile and run java -version again
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)
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.