Friday, October 31, 2008

Notes on SqliteJDBC and Java Web Start

Java has a long standing limitation about loading native code from a jar, stemming from the unfortunate belief that native code was somehow impure. In the practical world, there was occasional need to do this, so people came up with the work-around of copying the native library out of the jar and into the local filesystem where it could then be loaded with System.load().

Another way do do this, within web start is to platform specific resource elements like these to your jnlp:

<resources os="Windows" arch="x86">
  <j2se href="" version="1.5+"/>
  <nativelib href="sqlitejdbc-v052-native-win.jar"/>
<resources os="Mac OS">
  <j2se href="" version="1.5+"/>
  <nativelib href="sqlitejdbc-v052-native-mac.jar"/>
<resources os="Linux">
  <j2se href="" version="1.5+"/>
  <nativelib href="sqlitejdbc-v052-native-lin.jar"/>

This way, we get the native library for the right platform, if all goes according to plan. One wacky detail is that System.loadLibrary("foo") munges the library name in some platform specific and poorly documented way. So, you need a libfoo.jnilib for OS X, for linux, and a foo.dll for windoze. It's kind-of a hassle to set up all these separate jars. And System.load(...) just tries to do what it's told without munging the name, so people still frequently use the trick described above.

SqliteJDBC does just that to load the native Sqlite library. SqliteJDBC also includes "pure Java" drivers, which are considerably slower for some operations, but act as a nice fallback when the proper native libraries aren't handy.

This all works well enough. But, on OS X 10.5, Java 6, it ends up falling back on the non-native drivers. After digging a bit, I found that the call to System.load(..) was failing with this exception:

java.lang.UnsatisfiedLinkError: /private/tmp/libsqlitejdbc-19214.lib: 
 at java.lang.ClassLoader$NativeLibrary.load(Native Method)
 at java.lang.ClassLoader.loadLibrary0(
 at java.lang.ClassLoader.loadLibrary(
 at java.lang.Runtime.load0(
 at java.lang.System.load(

The same thing works find when using Java 5 on the same machine and on Java 6 on Windoze, so I'm guessing the native library needs to be recompiled to work correctly with OS X's 64-bit Java 6.

A little spelunking into SqliteJDBC's code reveals that the author has kindly provided hooks where you can insert the path and name of your own driver using these system properties:

  • org.sqlite.lib.path

The driver will then attempt this:

System.load(new File(libpath, libname).getAbsolutePath());

More Resources:

1 comment:

  1. I think JDBC Driver is better than ODBC Driver, We can download it from: