Java Apps on OS X, Part II: Loading Libraries
In the last post, I talked about converting our Pure Java app into a Cocoa-Java app in order to get the system sleep/wake events to get posted to the application. I did run into a few problems with our application while trying to get to that point. So, in this post, I’ll discuss the problems getting JNI libraries to load properly in a Cocoa-Java app.
Our product makes use of a couple libraries that have native components to them. These libraries take care of some of the integration with the operating systems themselves, such as applying icons to files. In the Pure Java code, loading these libraries is simple, just execute System.loadLibrary ( "nativeLib" ) ; and you’re done.
When you make the switch to Cocoa-Java, though, this call suddenly starts blowing up with an UnsatisfiedLinkError. I spent over an hour fiddling with settings in the Info.plist and Xcode build settings before stumbling across a page on Apple’s Developer Site that discussed the problem.
If you are developing a Cocoa Java application, you need to load your JNI library using a different mechanism. If your library is called hello.jnilib, you should callSystem.load(NSBundle.mainBundle().pathForResource("hello", "jnilib", "Java"));
Note that this assumes that your library is located in Resources/Java/.
With that bit of information in hand, I was able to find the sections in our code where we were loading the native libraries, and update them as follows:
try
{
System.loadLibrary("Native");
}
catch ( UnsatisfiedLinkError e )
{
System.load(NSBundle.mainBundle().pathForResource("libNative", "jnilib"));
}
And thus were the issues with native libraries vanquished.
Related Posts:
- Java Apps on OS X, Part I: Sleep Events
- iPhone Apps
- Moveable Type on a ChRooted Server
- Application Design Theory
- Exercices dans l’absurdit




Leave a Comment