Load multiple dependent libraries with JNA

后端 未结 2 2048
小蘑菇
小蘑菇 2021-01-07 10:52

Is there a way in JNA to load multiple dependent libraries with Java?

I usually use Native.loadLibrary(...) to load one DLL. But I guess its not workin

2条回答
  •  离开以前
    2021-01-07 11:36

    Loading lib transient dependencies with JNA from JAR Resources.

    My resources folder res:

    res/
    `-- linux-x86-64
        |-- libapi.so
        |-- libdependency.so
    

    -

    MyApiLibrary api = (MyApiLibrary) Native.loadLibrary("libapi.so", MyApiLibrary.class, options);
    

    API Explodes: Caused by: java.lang.UnsatisfiedLinkError: Error loading shared library libdependency.so: No such file or directory

    Can be solved by loading dependencies beforehand by hand:

    import com.sun.jna.Library;
    
    Native.loadLibrary("libdependency.so", Library.class);
    MyApiLibrary api = (MyApiLibrary) Native.loadLibrary("libapi.so", MyApiLibrary.class, options);
    

    Basically you have to build dependency tree in reverse, by hand, by yourself.


    I recommend setting

    java -Djna.debug_load=true -Djna.debug_load.jna=true
    

    Furthermore, setting jna.library.path to Resource has no effect, because JNA extracts to filesystem, then it loads lib. Lib on filesystem can NOT access other libs within jar.

    Context class loader classpath. Deployed native libraries may be installed on the classpath under ${os-prefix}/LIBRARY_FILENAME, where ${os-prefix} is the OS/Arch prefix returned by Platform.getNativeLibraryResourcePrefix(). If bundled in a jar file, the resource will be extracted to jna.tmpdir for loading, and later removed (but only if jna.nounpack is false or not set).

    Javadoc

    RTFM and happy coding. JNA v.4.1.0

提交回复
热议问题