简体   繁体   English

JNI包装的DLL仅在从默认包中调用时才有效

[英]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. 我得到了用于访问名片扫描仪的DLL和JNI包装器。

So far I have only been able to call the API when all classes are in the default package. 到目前为止,我只能在所有类都在默认包中时调用API。 When I try to move classes to other packages I get an UnsatisfiedLinkError . 当我尝试将类移动到其他包时,出现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. 到目前为止,我还没有JNI的经验,想知道是否需要重写是否可以用其他方式组织项目的包装器。 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. JNI的问题在于,包含本机方法的类的标准类名与本机(C-)函数的方法签名紧密耦合。

The C-signature must be something like C签名必须类似于

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 . 重命名该类或将其移动到另一个程序包将更改预期的函数名,并导致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. 除非您有权访问本机源来更改函数名,否则所有与DLL捆绑在一起的类都必须保留在默认程序包中,否则所有您自己的类都可以放在您希望拥有它们的位置。

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. 获取在默认程序包中具有其本机方法的JNI程序包被认为是较差的样式,并且对于所接收软件的质量而言并不是一个好兆头。 And be prepared for further trouble than can come from using the default package, AFAIR eg tomcat had (has?) problems with those. 并准备比使用默认软件包AFAIR(例如tomcat)遇到的其他麻烦更大的麻烦。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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