Wednesday, February 5, 2014

Don't mess with /System/Library/Frameworks/JavaVM.framework/Versions/Current in Mac OS X

I had both JDK1.6 and JDK1.7 installed in OS X 10.9 (Mavericks).  I was trying to force java to use default JDK version to 1.6. I thought its going to be fine if I change the soft link:

/System/Library/Frameworks/JavaVM.framework/Versions/Current -> A

to this:

/System/Library/Frameworks/JavaVM.framework/Versions/Current -> CurrentJDK

where CurrentJDK points to /System/Library/Java/JavaVirtualMachines/1.6.0.jdk/Contents (the JDK 1.6).

But after doing so, I got this error when trying to compile an application that depended on AWT libraries:

java[5617:d07] Apple AWT Startup Exception : -[__NSCFString appendString:]: nil argument
java[5642:d07] Apple AWT Restarting Native Event Thread

Also "/usr/libexec/java_home" (which gives the java_home) was also giving "command not found" error.

After doing some googling, I found that I shouldn't mess with /System/Library/Frameworks/JavaVM.framework/Versions/Current. So it should be kept as it was (pointed to A). Rather just change the link of /usr/bin/java to /System/Library/Java/JavaVirtualMachines/1.6.0.jdk/Contents/Home/bin/java:

sudo rm /usr/bin/java
sudo ln -s /System/Library/Java/JavaVirtualMachines/1.6.0.jdk/Contents/Home/bin/java /usr/bin/java

Then /usr/libexec/java_home should automatically point to /System/Library/Java/JavaVirtualMachines/1.6.0.jdk/Contents/Home.

Now you should set your $JAVA_HOME in your ~/.bash_profile file like this:

export JAVA_HOME=$(/usr/libexec/java_home)

Then source the ~/.bash_profile file:

source ~/.bash_profile

You're all set now with your preferred java version.