Search code examples
javac++java-native-interfacejnafedora-25

loading a c++ library.so in java raises the error: undefined symbol: XOpenDisplay


This question follows this one. I am able to find the library but when it is loading it raises the following error

java.lang.UnsatisfiedLinkError: /home/name/Documents/Development/GitLocalRepo/hidden-mathLibrary/hidden/lib/Distribution/libgtengine.so.3.7: /home/name/Documents/Development/GitLocalRepo/hidden-mathLibrary/hidden/lib/Distribution/libgtengine.so.3.7: undefined symbol: XOpenDisplay

the code raising the error is the following:

public class Frame {

    static {
        System.loadLibrary( "gtengine" );   // <- error occurs here
    }

    public interface libWrapper extends Library {

    libWrapper INSTANCE = (libWrapper)
            Native.loadLibrary(
                    "gtengine",
                    libWrapper.class );

    Pointer FrameCstructor(String name, long parent,
            double x, double y, double z,
            double rotX, double rotY, double rotZ,
            double vX, double vY, double vZ,
            double angVx, double angVy, double angVz,
            double accX, double accY, double accZ,
            double angAccX, double angAccY, double angAccZ,
            boolean addToFramelist);
    }

    public static native Pointer FrameCstructor(String name, long parent,
        double x, double y, double z,
        double rotX, double rotY, double rotZ,
        double vX, double vY, double vZ,
        double angVx, double angVy, double angVz,
        double accX, double accY, double accZ,
        double angAccX, double angAccY, double angAccZ,
        boolean addToFramelist);

    private Pointer ptrToCFrame;

    public Frame(String name, int parent,
        double x, double y, double z,
        double rotX, double rotY, double rotZ,
        double vX, double vY, double vZ,
        double angVx, double angVy, double angVz,
        double accX, double accY, double accZ,
        double angAccX, double angAccY, double angAccZ,
        boolean addToFramelist) {

    System.out.println("library: " + System.getProperty( "java.library.path" ));

    ptrToCFrame = libWrapper.INSTANCE.FrameCstructor( name, parent,
            x, y, z, rotX, rotY, rotZ,
            vX, vY, vZ, angVx, angVy, angVz,
            accX, accY, accZ, angAccX, angAccY, angAccZ,
            addToFramelist );
    }
}

Is it due to the presence of some references to X11 in the library? It will be a huge job to remove it from the library! I am working on fedora 25.

Thanks


Solution

  • Take a look here:

    https://github.com/mkowsiak/jnicookbook/blob/master/recipes/recipeNo023/Makefile

    In this sample code you can see how to deal with JNI code that requires some other code to work.

    Just make sure that library with "XOpenDisplay" (xlib - as already mentioned by sithereal) is visible to JVM (e.g. add it to LD_LIBRARY_PATH or user -Wl,-rpath while building your JNI code).

    Have fun with JNI.