使用JNA加载多个依赖库 [英] Load multiple dependent libraries with JNA

查看:2254
本文介绍了使用JNA加载多个依赖库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

JNA有没有办法使用Java加载多个依赖库?

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

我通常使用 Native.loadLibrary(...)加载一个DLL。但是我猜这是不行的,因为我把这个函数调用给实例成员。

I usually use Native.loadLibrary(...) to load one DLL. But I guess its not working this way because I assign this function call to the instance member.

推荐答案

code> foo 和库 bar bar 依赖于 foo ;它也依赖于 baz ,我们是不使用JNA映射:

Let's say I have library foo and library bar. bar has a dependency on foo; it also has a dependency on baz, which we are not mapping with JNA:

public class Foo {
    public static final boolean LOADED;
    static {
        Native.register("foo");
        LOADED = true;
    }
    public static native void call_foo();
}

public class Bar {
    static {
        // Reference "Foo" so that it is loaded first
        if (Foo.LOADED) {
            System.loadLibrary("baz");
            // Or System.load("/path/to/libbaz.so")
            Native.register("bar");
        }
    }
    public static native void call_bar();
}

调用 System.load / loadLibrary 只有在 baz 不在您的库加载路径( PATH / LD_LIBRARY_PATH ,分别用于Windows / linux),也不在与 bar相同的目录中(仅限Windows)。

The call to System.load/loadLibrary will only be necessary if baz is neither on your library load path (PATH/LD_LIBRARY_PATH, for windows/linux respectively) nor in the same directory as bar (windows only).

编辑

您还可以通过界面映射来执行此操作:

You can also do this via interface mapping:

public interface Foo extends Library {
    Foo INSTANCE = (Foo)Native.loadLibrary("foo");
}
public interface Bar extends Library {
    // Reference Foo prior to instantiating Bar, just be sure
    // to reference the Foo class prior to creating the Bar instance
    Foo FOO = Foo.INSTANCE;
    Bar INSTANCE = (Bar)Native.loadLibrary("bar");
}

这篇关于使用JNA加载多个依赖库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆