Log in

No account? Create an account
Bigger hammer

How to force Java applications to run in 32 bit mode on Mac OS X

I'm writing this mainly so that Google will index it and hopefully it will provide the solution for anybody else who runs into the same problem.

If you have a Java application on Mac OS X that is contained in an application bundle created using Jar Bundler, it includes a native executable in your.app/Contents/MacOS/JavaApplicationStub that automatically runs the JRE in 64 bit mode when you run it on a 64 bit capable Mac. You can check if this has happened by running your application and looking at the Kind column in Activity Monitor.

My problem was that my Java application was trying to integrate with Open Office, which is currently a 32 bit only application. My program, running in 64 bit mode, tried to dynamically load the OO libraries and failed because they are only available in 32 bit format. I didn't discover this during development because I have a 2006 vintage Mac Mini that only has a 32 bit processor and a 2003 vintage iBook that has a G4 PPC processor. One of my beta testers had a newer Mac and was unable to get the program to output anything to OpenOffice.

Incidentally the error messages that enabled me to figure out what was going wrong were logged to system.log (visible using /Applications/Utilities/Console.app). I think this is quite a nice feature of JavaApplicationStub.

The quick workaround for the problem is to right-click on your .app bundle and select Get Info, then click on the "Run in 32 bit mode" box. This seems to be a local setting only though - copy the .app to a different machine and the setting is lost - so it doesn't work for distributing your application to other people.

A bit of background: OSX executable files are fat binaries. That means they can contain binary code suitable for several processor architectures. When you run the file the operating system automatically chooses the most appropriate one for you. JavaApplicationStub contains three architectures: ppc (PowerPC - the type of processor Macs used to have before 2006), i386 (32 bit Intel) and x86_64 (64 bit Intel). If you run it on a 64 bit Intel Mac, the OS will use x86_64.

The permanent solution I found was to strip the x86_64 executable section out of JavaApplicationStub. Apple provides a program called lipo which allows you to do all sorts of clever stuff with the different sections in a fat binary. See man lipo for more information.

From a Terminal window I cd'ed into the Contents/MacOS/ folder inside my application bundle and ran the following commands:

mv JavaApplicationStub JavaApplicationStub64
lipo JavaApplicationStub64 -remove x86_64 -output JavaApplicationStub

This keeps the original 64 bit capable version in case OpenOffice.org later releases a 64 bit version of OO and I again want to be able to run my application in 64 bit mode. You can check that the x86_64 bit section has gone by running lipo -info:

alex$ lipo -info JavaApplicationStub
Architectures in the fat file: JavaApplicationStub are: i386 ppc
alex$ lipo -info JavaApplicationStub64
Architectures in the fat file: JavaApplicationStub64 are: x86_64 i386 ppc