title: Load DLL (using JNA) inside an OSGi bundle
body: <p>OSGi cannot find my DLL file, and I can't seem to figure out why.</p>
  
  <p>Currently I have the DLL file (<code>foo.dll</code>) at the root of my bundle, I've also tried having it in a <code>libs</code> directory.</p>
  
  <p>The Manifest for the bundle in question looks something like this:</p>
  
  <pre><code>Manifest-Version: 1.0
  Bundle-ManifestVersion: 2
  Bundle-Name: foobundle
  Bundle-SymbolicName: com.foo.bar
  Bundle-Version: 1.0.0
  Bundle-Vendor: me
  Import-Package: com.sun.jna,
   com.sun.jna.ptr,
   com.sun.jna.win32
  Export-Package: com.foo.bar
  Bundle-NativeCode: foo.dll;
   osname=WindowsXP;
   processor=x86
  </code></pre>
  
  <p>Then in my JNA interface I perform a loadLibrary (as per the documentation):</p>
  
  <pre><code>public interface MyFooInterface extends com.sun.jna.Library{
      static final MyFooInterface INSTANCE = (MyFooInterface)com.sun.jna.Native.loadLibrary("foo", MyFooInterface .class);
  
      // specific interface defs here...
  }
  </code></pre>
  
  <p>Then in another class I attempt to use the JNA interface</p>
  
  <pre><code>// ...code
  int var = MyFooInterface.INSTANCE.bar();
  // ...more code
  </code></pre>
  
  <p>I have JNA supplied via another bundle (which exports com.sun.jna and the other packages imported above), but have also tried packaging it with the bundle defined here (and added it to the classpath in that case, etc.).</p>
  
  <p>I've also tried specifying <code>Bundle-NativeCode: /foo.dll</code>.</p>
  
  <p>Also of interest, these are the relevant OSGi properties (which I pulled up using <code>getprop</code>)</p>
  
  <pre><code>org.osgi.framework.os.name=WindowsXP
  org.osgi.framework.processor=x86
  </code></pre>
  
  <p>Even after all this (and with every trial I made) I always end up with the following error (and a stack trace not shown):</p>
  
  <pre><code>java.lang.UnsatisfiedLinkError: Unable to load library 'foo': The specified module could not be found.
  </code></pre>
  
  <p>...so what am I missing?</p>
  
  <p><em>Edit</em>: I should also note that I've tested and had success the JNA interface code and the DLL that it talks to as part of a JUnit Test program.</p>
  
  <p><em>Edit 2</em>: Adding this code to the class that's calling the library seems to allow JNA to find the library (when <code>Native.loadLibrary</code> gets called later).  It seems I should be able to avoid this call based on the Bundle-NativeCode directive in the Manifest.  Clearly once the library is loaded Native.loadLibrary grabs the existing instance of it, but I'd prefer not to depend on this very order-specific tactic.</p>
  
  <pre><code>static{
      System.loadLibrary("foo");
  }
  </code></pre>