简体   繁体   中英

JNI wrapped DLL only works when called from the default package

I have been given a DLL and a JNI wrapper which are used to access a business card scanner.

So far I have only been able to call the API when all classes are in the default package. When I try to move classes to other packages I get an UnsatisfiedLinkError .

I have had no experience of JNI up until now and wonder if I need to rewrite the wrapper of if I can organise the project in a different way. What I cannot do is to use the default package as this would mean putting all my classes there.

The wrapper looks something like this:

public final class Wrapper {
    private native int CRTK_Init(int[] lphRTK);

    private int m_hRTK;
    private int m_hRTKDB;

    static
    {
        System.loadLibrary("crtk_jni");
    }

    public Wrapper() {
        m_hRTKDB = 0;
        int[] pRTK = new int[1];
        CRTK_Init(pRTK);  // UnsatisfiedLinkError here
        m_hRTK = pRTK[0];
    }
}

The thing with JNI is that the fully qualified class name of the class containing the native methods is tightly coupled to the method-signature of the native (C-)functions.

The C-signature must be something like

JNIEXPORT jobject JNICALL Java_packageName_className_methodName(JNIEnv * env, jclass parameter)

Renaming the class or moving it to another package would change the expected function-name and result in an UnsatisfiedLinkError .

So what can you do?

Unless you have access to the native sources to change the function-names all classes that come as a bundle together with the dll must remain in the default-package, all your own classes can go where you want to have them.

Getting a JNI-package that has its native methods in the default-package is considered poor style and does not bode well for the quality of the received software. And be prepared for further trouble than can come from using the default package, AFAIR eg tomcat had (has?) problems with those.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM