Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UnsatisfiedLinkError with native library under sbt

Tags:

scala

sbt

I'm using sbt 0.13 and have issues using the leveldbjni native library under sbt (even after issue #358 has been resolved). A similar issue has already been reported for which sbt 0.13 should provide a solution but it seems it doesn't. So I'm sharing my observations here.

I'm getting an UnsatisfiedLinkError with the following example application.

  • build.sbt

    name := "example"
    
    version := "0.1"
    
    scalaVersion := "2.10.2"
    
    libraryDependencies += "org.fusesource.leveldbjni" % "leveldbjni-all" % "1.7"
    
  • build.properties

     sbt.version=0.13.0
    
  • Example.scala

    import org.fusesource.leveldbjni.internal._
    
    object Example extends App {
      NativeDB.LIBRARY.load() // loading succeeds 
      new NativeOptions() // UnsatisfiedLinkError under sbt
    }
    

I'm using Oracle JDK 1.7 and OS X 10.8.5. Running the example with run-main Example under sbt gives

[error] (run-main) java.lang.UnsatisfiedLinkError: org.fusesource.leveldbjni.internal.NativeOptions.init()V

whereas running it with

java -cp scala-library.jar:example_2.10-0.1.jar:leveldbjni-all-1.7.jar Example

just works fine. The application even runs successfully when Scala is on the bootclasspath:

java -Xbootclasspath/a:scala-library.jar -cp example_2.10-0.1.jar:leveldbjni-all-1.7.jar Example

Any ideas why there's an UnsatisfiedLinkError only under sbt?

like image 254
Martin Krasser Avatar asked Oct 17 '13 11:10

Martin Krasser


1 Answers

Any ideas why there's an UnsatisfiedLinkError only under sbt?

As @juereth commented:

Sbt is using magic classloaders to try to prevent "leaks" of native loaded libraries, and allow you to re-import the library multiple times. Sbt will actually rename the DLL/SO/jnilib and load it in a special classloader. I'm not positive if this is only a plugin feature, or something for all apps.

So this is somewhat expected behavior with the current design of sbt. If forking works, then that's what you have to do.

like image 108
Eugene Yokota Avatar answered Nov 10 '22 19:11

Eugene Yokota