wurmi wurmi - 1 year ago 119
Java Question

Load multiple dependent libraries with JNA

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

I usually use

to load one DLL. But I guess its not working this way because I assign this function call to the instance member.

Answer Source

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 {
        LOADED = true;
    public static native void call_foo();

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

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");
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download