另一个 JNI、C++、DLL、UnsatisfiedLinkError <Native Method>

Another JNI, C++, DLL, UnsatisfiedLinkError lt;Native Methodgt;(另一个 JNI、C++、DLL、UnsatisfiedLinkError lt;Native Methodgt;)

本文介绍了另一个 JNI、C++、DLL、UnsatisfiedLinkError <Native Method>的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经找了 2 天了,没有任何解决方案可以帮助我,所以我们又来了:

I've been looking for 2 days now and no solution could help me, so here we go again:

如何修复 JNI 中的 UnsatisfiedLinkError...?

How to fix the UnsatisfiedLinkError... in JNI?

这是我的java代码:

So here's my java code:

package org.lingenio.util;

import java.util.*;

public class PTAPIWrapperForOmegaT {

    private native String translateWithPTAPI(String sentence);

    private native void test();

    public PTAPIWrapperForOmegaT(String sentence) throws Exception{
        System.out.println(sentence);
        test();     
    }

    static {
        System.load("C:/Users/michael/Desktop/OmegaT/OmegaT2.3_src/native/PTAPIWrapperForOmegaT.dll");
    }
}

这是我的 C++ 代码:

And here's my C++ Code:

    #include <iostream>
    #include <windows.h>
    #include <jni.h>
    #include "PTAPIWrapperForOmegaT.h"

    using namespace std;

    JNIEXPORT jstring JNICALL Java_PTAPIWrapperForOmegaT_translateWithPTAPI(JNIEnv *env, jobject obj, jstring sentence)
    {
/* stuff */
    }

    JNIEXPORT void JNICALL Java_PTAPIWrapperForOmegaT_test(JNIEnv *, jobject)
    {
        cout << "This comes from PTAPIWrapperForOmegaT.cpp test();" << endl;
    }


    int main(){
        return 0;
    }

还有头文件:

/* DO NOT EDIT THIS FILE - it is machine generated */
#include <jni.h>
/* Header for class PTAPIWrapperForOmegaT */

#ifndef _Included_PTAPIWrapperForOmegaT
#define _Included_PTAPIWrapperForOmegaT
#ifdef __cplusplus
extern "C" {
#endif
/*
 * Class:     PTAPIWrapperForOmegaT
 * Method:    translateWithPTAPI
 * Signature: (Ljava/lang/String;)Ljava/lang/String;
 */
JNIEXPORT jstring JNICALL Java_PTAPIWrapperForOmegaT_translateWithPTAPI
  (JNIEnv *, jobject, jstring);

/*
 * Class:     PTAPIWrapperForOmegaT
 * Method:    test
 * Signature: ()V
 */
JNIEXPORT void JNICALL Java_PTAPIWrapperForOmegaT_test
  (JNIEnv *, jobject);

#ifdef __cplusplus
}
#endif
#endif

以及我如何构建它:

call g++ -Wl,--add-stdcall-alias -c -DBUILDING_EXAMPLE_DLL -I G:/Software/Java/jdk1.7.0_01/include -I G:/Software/Java/jdk1.7.0_01/include/win32 PTAPIWrapperForOmegaT.cpp
call g++ -shared -Wl,-kill-at -o PTAPIWrapperForOmegaT.dll -I G:/Software/Java/jdk1.7.0_01/include -I G:/Software/Java/jdk1.7.0_01/include/win32 PTAPIWrapperForOmegaT.cpp

最后是错误:

10211: Error: Uncatched exception in thread [Thread-14] 
10211: Error: java.lang.UnsatisfiedLinkError: org.lingenio.util.PTAPIWrapperForOmegaT.test()V 
10211: Error:   at org.lingenio.util.PTAPIWrapperForOmegaT.test(Native Method) 
10211: Error:   at org.lingenio.util.PTAPIWrapperForOmegaT.<init>(PTAPIWrapperForOmegaT.java:13) 
10211: Error:   at org.omegat.core.machinetranslators.LingenioTranslate.translate(LingenioTranslate.java:32) 
10211: Error:   at org.omegat.core.machinetranslators.BaseTranslate.getTranslation(BaseTranslate.java:64) 
10211: Error:   at org.omegat.gui.exttrans.MachineTranslateTextArea$FindThread.search(MachineTranslateTextArea.java:122) 
10211: Error:   at org.omegat.gui.exttrans.MachineTranslateTextArea$FindThread.search(MachineTranslateTextArea.java:102) 
10211: Error:   at org.omegat.gui.common.EntryInfoSearchThread.run(EntryInfoSearchThread.java:85) 

我不完全了解这两行 g++ ,我认为第二行就足够了,但某些教程一定也提供了另一行,我保留了它.

I don't know exactly about these two lines of g++ here, I think the second one would be sufficient, but some tutorial must have offered the other line as well and I kept it.

我在 Windows 7 上,使用 MingW 和最新的 Java(我相信是 1.7xxx).

I'm on Windows 7, using MingW and the latest Java (1.7xxx I believe).

感谢任何帮助,我怀疑错误在于编译,但我只是不知道如何从这里继续.

Any help is appreciated, I suspect the error lies in the compilation, but I just don't know how to go on from here.

编辑:

使用 DependencyWalker 查看 dll,我可以看到函数的命名方式与我在 .cpp 文件中的命名方式相同.当然,我是从 Java Wrapper 中用它们各自的名称调用它们的,即 test().这会是个问题吗?过去经常使用JNI的人可以告诉我这是否正确吗?

Looking into the dll with DependencyWalker I can see the functions are named like I named them in the .cpp file. Of course I am calling them from the Java Wrapper with their respective names, i.e. test(). Could that be a problem? Can someone who used JNI often in the past tell me whether this is the correct way?

推荐答案

原来所有的代码都很好.实际上我确实在编译头文件时犯了错误.你可以看看你是否查看头文件的函数名,即:

Turns out all the code is fine. Actually I did make mistakes compiling the header files. You can see if you look at the header files' function names, i.e.:

JNIEXPORT jstring JNICALL Java_PTAPIWrapperForOmegaT_translateWithPTAPI
  (JNIEnv *, jobject, jstring);

现在,看看你的 Java 文件的包成员资格,在我的例子中:

Now, take a look at your Java files' package membership, in my case:

package org.lingenio.util;

因为我确实把头文件编译错了,JNI后来没能找到它要找的符号,因为它实际上是在找这个:

Because I did compile the header file the wrong way, JNI was later not able to find the symbols it was looking for, because it was actually looking for this:

JNIEXPORT jstring JNICALL Java_org_lingenio_util_PTAPIWrapperForOmegaT_translateWithPTAPI(JNIEnv *env, jobject obj, jstring sentence)

所以,祝那些遇到同样问题的人好运.我显然不是最伟大的 Java 程序员,这就是为什么我不得不为此担心这么久.我应该首先以正确的方式编译我的头文件.检查你的包和类路径!

So, good luck to the people out there dangling with the same problems. I'm obviously not the greatest Java programmer, that's why I had to worry about this for so long. I should have compiled my header files in the correct way in the first place. Check your package and classpath!

这篇关于另一个 JNI、C++、DLL、UnsatisfiedLinkError &lt;Native Method&gt;的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:另一个 JNI、C++、DLL、UnsatisfiedLinkError &lt;Native Method&gt;

基础教程推荐