January 28, 2011

Add APR Based Native Library for Tomcat on Mac OSX

We can enhance Tomcat's features by using the APR based native library with Tomcat. If we put this library in the Java library path we get better performance, secure session ID support and OS level statistics. The Tomcat documentation explains how to achieve this by compiling sources and the like. But with Mac OSX and MacPorts we don't have to do this ourselves.

With the following steps I was able to add the native library to Tomcat:

  1. Install the Java developer package from Apple's Developer Connection
  2. Add a link with the name include in the /System/Library/Frameworks/JavaVM.framework/Home/ directory. We need this step because later on when we install tomcat-native with MacPorts this path is used as an include for compiling the source files.
    $ ln -s /System/Library/Frameworks/JavaVM.framework/Headers include
  3. $ sudo port install apr
  4. $ sudo port install tomcat-native

After these steps the APR based native libraries are installed in /opt/local/lib. So all we have to do is add this path to the Java library path when we start Tomcat:

$ export CATALINA_OPTS=-Djava.library.path=/opt/local/lib
$ bin/catalina.sh run

Alternatively we can create a symbolic link to add the native library to the default Java installation, so we don't have to specify the /opt/local/lib directory as a Java library path.

$ sudo ln -s /opt/local/lib/libtcnative-1.* /usr/lib/java

The following links helped me a lot to add the APR native library to my system: