Load DLL (using JNA) inside an OSGi bundle

后端 未结 3 863
陌清茗
陌清茗 2020-12-10 07:37

OSGi cannot find my DLL file, and I can\'t seem to figure out why.

Currently I have the DLL file (foo.dll) at the root of my bundle, I\'ve also tried ha

相关标签:
3条回答
  • 2020-12-10 08:14

    I suggest you try to package the dll as a jar:

    jar cvf foo.dll.jar foo.dll
    

    and the load the jar as a regular lib.

    0 讨论(0)
  • 2020-12-10 08:18

    The problem is the specialised JNA loadLibrary call, which is not OSGi aware. When you invoke loadLibrary from an OSGi bundle, it will use the OSGi classloader (which is bundle aware) to find where the DLL is, and in this case, extract it out from the bundle and make it loadable via the System.loadLibrary() call against a specific location.

    Since this JNA seems to be (a) not OSGi aware, and (b) superflous, why not just use System.loadLibrary() instead?

    If you need to write both, then perform a System.loadLibrary() in the bundle's start() method in the BundleActivator, which will bring the native library in (you probably want to ensure that if it can't be loaded, the bundle can't be started in any case).

    0 讨论(0)
  • 2020-12-10 08:31

    Looking at JNA's documentation, it states:

    • Make your target library available to your Java program. There are two ways to do this:
      • The preferred method is to set the jna.library.path system property to the path to your target library. This property is similar to java.library.path but only applies to libraries loaded by JNA.
      • Change the appropriate library access environment variable before launching the VM. This is PATH on Windows, LD_LIBRARY_PATH on Linux, and DYLD_LIBRARY_PATH on OSX.

    So to get around this shortcoming you could resolve the the absolute path of the library and load that.

    Assuming that its Eclipse's standard class loader, you can do ClassLoader.findLibrary() which should find the local library in the bundle.

    0 讨论(0)
提交回复
热议问题