Thursday, June 18, 2009

Java for OS X Update 4, Java Preferences and JAVA_HOME

Java for Mac OS X 10.5 Update 4 is out, and if you're on Mac OS X 10.5, you should install it as soon as possible; it patches the serious vulnerabilities that existed in Java for OS X before that.

However, if you're unfortunate, installing this will also mess up your ability to run different versions of Java. On OS X, the version of Java you run by default can be controlled using the Java Preferences panel:

Unfortunately, I quickly discovered that this was no longer true on my system. Although Java 1.6 was heading up the pack, Java 1.5 seemed to be the VM of choice when compiling and running Java code. And since I'm on a project that's using Java 1.6, this was a serious problem.

Changing the value in the Java Preferences application had no effect. Neither did rebooting. I briefly considered reconfiguring the Java setup using softlinks, but it seemed like that was a last resort as it would be further breaking the Java preferences panel. I did some digging and didn't find very much at first.

Eventually I discovered this post on Apple's "java-dev" mailing list talking about changes to the way JAVA_HOME works on OS X, and started experimenting. I discovered:
  • The value of JAVA_HOME was pointing to Java 1.5 on my system.
  • Altering JAVA_HOME had an immediate and significant impact on what version of Java was used to run 'java' or 'javac' by the executeables that were softlinked by OS X.
  • The output of the new 'java_home' command was influenced by whatever went into the Java Preferences panel.
Accordingly, I did as the mailing list suggested, and set JAVA_HOME to `/usr/libexec/java_home` and voila, everything seems to have resolved itself. If any of you run into this, hopefully you'll find success with this solution as well.

. In order to better support JAVA_HOME in OSX there have been some changes. Out of curiosity, I switched the order of the JVMs in Java Preferences and ran the new 'java_home' command and the results would change. I also tried changing the value of the JAVA_HOME environment variable, which immediately affected which version of Java would be run by the 'java' and 'javac' commands.

I'm guessing that I had a JAVA_HOME setting already, pointing to Java 1.5, and it was this value that was overriding whatever was in my Java Preferences. Adjusting JAVA_HOME to be the result of the java_home invocation seems to have solved my problems.

I'm hoping that by posting this I can save some of you the same pain.


ncr100 said...

You are AWESOME, dude!

Anonymous said...

Yeah, you saved my day. Don't understand why these problems haven't surfaced on more people's machines.

Geoffrey Wiseman said...

The post has gotten a fair amount of traffic, so I imagine it's surfaced for some, but, yes, I'm surprised there weren't more people with this problem complaining about it, talking about it, which would have made my life easier. ;)

Mul said...

Seems as though this is a step in the right direction. Prior to the latest release (update 4) the JAVA_HOME variable was ignored by the /usr/bin/java link. Controlling the version exclusively from the Java Preferences app always seemed very "non Java", as the accepted standard is to use whatever JAVA_HOME points to. Using the env variable now makes it much easier to change the default version programatically.