title: Scala SBT and JNI library
body: <p>I am writing a simple app in <code>Scala</code> that uses a leveldb database through the <code>leveldbjni</code> library. My <code>build.sbt</code> file looks like this:</p>
  
  <pre><code>name := "Whatever"
  
  version := "1.0"
  
  scalaVersion := "2.10.2"
  
  libraryDependencies ++= Seq(
      "org.iq80.leveldb" % "leveldb-api" % "0.6",
      "org.fusesource.leveldbjni" % "leveldbjni-all" % "1.7"
  )
  </code></pre>
  
  <p>An <code>Object</code> is then responsible for creating a database. Unfortunately if I run the program I get back a <code>java.lang.UnsatisfiedLinkError</code>, raised by the <code>hawtjni</code> library that <code>leveldbjni</code> exploits under the hood.</p>
  
  <p>The error can be triggered easily also from the scala console:</p>
  
  <pre><code>scala&gt; import java.io.File
  scala&gt; import org.iq80.leveldb._
  scala&gt; import org.fusesource.leveldbjni.JniDBFactory._
  scala&gt; factory.open(new File("test"), new Options().createIfMissing(true))
  
  java.lang.UnsatisfiedLinkError: org.fusesource.leveldbjni.internal.NativeOptions.init()V
      at org.fusesource.leveldbjni.internal.NativeOptions.init(Native Method)
      at org.fusesource.leveldbjni.internal.NativeOptions.&lt;clinit&gt;(NativeOptions.java:54)
      at org.fusesource.leveldbjni.JniDBFactory$OptionsResourceHolder.init(JniDBFactory.java:98)
      at org.fusesource.leveldbjni.JniDBFactory.open(JniDBFactory.java:167)
      at .&lt;init&gt;(&lt;console&gt;:15)
  ...
  scala&gt; System getProperty "java.io.tmpdir"
  res2: String = /var/folders/1l/wj6yg_wd15sg_gcql001wchm0000gn/T/
  </code></pre>
  
  <p>I can't understand what is going on since the library is getting correctly extracted from the jar file but it is not getting loaded for some reasons. </p>
  
  <pre><code>$ file /var/folders/1l/wj6yg_wd15sg_gcql001wchm0000gn/T/lib*
  /var/folders/1l/wj6yg_wd15sg_gcql001wchm0000gn/T/libleveldbjni-1.7.jnilib:    Mach-O universal binary with 2 architectures
  /var/folders/1l/wj6yg_wd15sg_gcql001wchm0000gn/T/libleveldbjni-1.7.jnilib (for architecture x86_64):    Mach-O 64-bit dynamically linked shared library x86_64
  /var/folders/1l/wj6yg_wd15sg_gcql001wchm0000gn/T/libleveldbjni-1.7.jnilib (for architecture i386):  Mach-O dynamically linked shared library i386
  </code></pre>
  
  <p>I think the problem is probably related to the classloader that sbt employs but I am not sure since I am relatively new to scala.</p>
  
  <p><strong>UPDATE</strong></p>
  
  <p>Still didn't find what or who is the culprit. Anyway the library is actually found and correctly loaded, since I can execute the following commands:</p>
  
  <pre><code>scalac&gt; import org.fusesource.leveldbjni.internal.NativeDB
  scalac&gt; NativeDB.LIBRARY.load()
  </code></pre>
  
  <p>The error is somehow due to the <code>init()</code> function that according to the <code>hawtjni</code> <a href="http://fusesource.com/forge/sites/hawtjni/documentation/developer-guide.html" rel="noreferrer">documentation</a> is responsible for setting <em>all the static fields annotated as constant fields with the constant value</em>. The exception can still be triggered by typing:</p>
  
  <pre><code>scalac&gt; import org.fusesource.leveldbjni.internal.NativeOptions
  scalac&gt; new NativeOptions()
  java.lang.UnsatisfiedLinkError: org.fusesource.leveldbjni.internal.NativeOptions.init()V
      at org.fusesource.leveldbjni.internal.NativeOptions.init(Native Method)
      at org.fusesource.leveldbjni.internal.NativeOptions.&lt;clinit&gt;(NativeOptions.java:54)
      at .&lt;init&gt;(&lt;console&gt;:9)
  </code></pre>