Search code examples
javajava-native-interface

How to get rid of LD_PRELOAD when using JNI


I have JNI library libA.so, which depends on a C library libB.so.

In Java, one way to resolve "symbol lookup error" is to have:

class A{
    public native void g();
    static{
        System.load("/asdfghjk/libA.so");
    }
}

Then run the java program with

LD_PRELOAD=libB.so

However, this hack cannot be used in production system, for example when the program is deployed with Tomcat JSF.

The following "solution" doesn't work:

class A{
    public native void g();
    static{
        System.load("/asdfghjk/libB.so");
        System.load("/asdfghjk/libA.so");
    }
}

It produces an error:

symbol lookup error: /blah/... undefined symbol: _ZNblahblahblah...

Solution

  • That error is because it can't find libraries which your shared library depends on.

    You should either add the location of dependent libraries to your java.library.path, explicitly System.load them first in the right order, or somehow use the dlopen system call to load the dependencies.

    This question may be helpful: Java: load shared librariees with dependencies