Wednesday, October 29, 2014

Multiple java versions on Mac OS

I like alternatives utility on linux which allows managing multiple versions of the same application. Unfortunately Mac OS does not have such utility therefore one needs to manage dependencies on his own. For java dev such important dependency is obviously java.

It took me a while to understand the directory structure of java versions 6, 7 and 8 which are installed on a newly released Mac OS 10.10 Yosemite.

It is known that Oracle does not support java 6 for Mac OS and instead Apple has its own version: http://support.apple.com/kb/dl1572

After installing it or updating to Yosemite the java directory layout looks like that:

$ which java
/usr/bin/java

$ ls -laF /usr/bin/java
lrwxr-xr-x 1 root wheel 77 Oct 29 20:34 /usr/bin/java@ -> /System/Library/Frameworks/JavaVM.framework/Versions/Current/Commands/java

$ ls -laF /System/Library/Frameworks/JavaVM.framework/Versions
drwxr-xr-x 11 root wheel 374 Oct 29 20:33 ./
drwxr-xr-x 10 root wheel 340 Oct 29 20:20 ../
lrwxr-xr-x 1 root wheel 10 Oct 29 20:20 1.4@ -> CurrentJDK
lrwxr-xr-x 1 root wheel 10 Oct 29 20:20 1.4.2@ -> CurrentJDK
lrwxr-xr-x 1 root wheel 10 Oct 29 20:20 1.5@ -> CurrentJDK
lrwxr-xr-x 1 root wheel 10 Oct 29 20:20 1.5.0@ -> CurrentJDK
lrwxr-xr-x 1 root wheel 10 Oct 29 20:20 1.6@ -> CurrentJDK
lrwxr-xr-x 1 root wheel 10 Oct 29 20:20 1.6.0@ -> CurrentJDK
drwxr-xr-x 7 root wheel 238 Oct 29 20:20 A/
lrwxr-xr-x 1 root wheel 54 Oct 29 20:27 Current@ -> /System/Library/Frameworks/JavaVM.framework/Versions/A
lrwxr-xr-x 1 root wheel 58 Oct 29 20:33 CurrentJDK@ -> /System/Library/Java/JavaVirtualMachines/1.6.0.jdk/Contents


After installing java 7 and 8 JDK, they land to the following directories:

$ ls -laF /Library/Java/JavaVirtualMachines/
drwxr-xr-x 5 root wheel 170 Oct 29 20:39 ./
drwxrwxr-x 5 root wheel 170 Oct 29 20:20 ../
drwxr-xr-x 3 root wheel 102 Oct 23 21:28 jdk1.7.0_71.jdk/
drwxr-xr-x 3 root wheel 102 Oct 29 20:39 jdk1.8.0_25.jdk/


Note the path difference between /Library/Java/JavaVirtualMachines/ for java 7, 8 and /System/Library/Java/JavaVirtualMachines for java 6

Now we need to make these versions live together and provide flexibility for choosing necessary default java version.
One can do it by following next two steps.

Step 1: Map CurrentJDK to the necessary version

I need java 7 to be default, so

$ sudo rm -f /System/Library/Frameworks/JavaVM.framework/Versions/CurrentJDK
$ sudo ln -s /Library/Java/JavaVirtualMachines/jdk1.7.0_71.jdk/Contents /System/Library/Frameworks/JavaVM.framework/Versions/CurrentJDK

Step 2: Remap /usr/bin/ to /System/Library/Frameworks/JavaVM.framework/Versions/CurrentJDK/Home/bin/

I have written a small python2.7 script for migrating binaries to CurrentJDK: remap java.
python remap_java.py
NOTE: the script requires root privileges

That is it!

For making default another version, we need to repeat the first step. For example, to change to java 8 do:

$ sudo rm -f /System/Library/Frameworks/JavaVM.framework/Versions/CurrentJDK
$ sudo ln -s /Library/Java/JavaVirtualMachines/jdk1.8.0_25.jdk/Contents /System/Library/Frameworks/JavaVM.framework/Versions/CurrentJDK

$ java -version
java version "1.8.0_25"
Java(TM) SE Runtime Environment (build 1.8.0_25-b17)
Java HotSpot(TM) 64-Bit Server VM (build 25.25-b02, mixed mode)


Happy coding!

No comments:

Post a Comment