另一个JNI,C ++,DLL,UnsatisfiedLinkError< Native方法> [英] Another JNI, C++, DLL, UnsatisfiedLinkError <Native Method>
问题描述
我一直在寻找2天,没有解决方案可以帮助我,所以这里我们再次:
如何解决UnsatisfiedLinkError ...在JNI ?
这是我的java代码:
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 ++代码:
#include< iostream>
#include< windows.h>
#include< jni.h>
#includePTAPIWrapperForOmegaT.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< 这来自PTAPIWrapperForOmegaT.cpp test(); << endl;
}
int main(){
return 0;
}
和头文件:
/ *不要编辑这个文件 - 它是机器生成的* /
#include< jni.h>
/ * PTAPIWrapperForOmegaT类的标题* /
#ifndef _Included_PTAPIWrapperForOmegaT
#define _Included_PTAPIWrapperForOmegaT
#ifdef __cplusplus
externC{
#endif
/ *
*类:PTAPIWrapperForOmegaT
*方法:translateWithPTAPI
*签名:(Ljava / lang / String;)Ljava / lang / String;
* /
JNIEXPORT jstring JNICALL Java_PTAPIWrapperForOmegaT_translateWithPTAPI
(JNIEnv *,jobject,jstring);
/ *
*类:PTAPIWrapperForOmegaT
*方法:test
*签名:()V
* /
JNIEXPORT void JNICALL Java_PTAPIWrapperForOmegaT_test
(JNIEnv *,jobject);
#ifdef __cplusplus
}
#endif
#endif
以及如何构建它:
调用g ++ -Wl, - add-stdcall-alias - c -DBUILDING_EXAMPLE_DLL -IG:/Software/Java/jdk1.7.0_01/include -IG:/Software/Java/jdk1.7.0_01/include/win32 PTAPIWrapperForOmegaT.cpp
调用g ++ -shared -Wl,-kill- at -o PTAPIWrapperForOmegaT.dll -IG:/Software/Java/jdk1.7.0_01/include -IG:/Software/Java/jdk1.7.0_01/include/win32 PTAPIWrapperForOmegaT.cpp
,最后出现错误:
10211:错误:线程中未捕获的异常[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:错误:在org.omegat.core.machinetranslators.BaseTranslate.getTranslation(BaseTranslate.java:64)
10211:错误:在org.omegat.gui.exttrans.MachineTranslateTextArea $ FindThread.search(MachineTranslateTextArea.java:122)
10211:错误:在org.omegat.gui.exttrans.MachineTranslateTextArea $ FindThread.search(MachineTranslateTextArea.java:102)
10211:错误:在org.omegat.gui.common.EntryInfoSearchThread.run(EntryInfoSearchThread.java:85)
我不知道这两行g ++在这里,我认为第二个是足够的,但一些教程必须提供其他行,以及保持它。
我在Windows 7,使用MingW和最新的Java(我相信1.7xxx)。
任何帮助,
编辑:使用DependencyWalker查看dll我可以看到这些函数的命名类似于我在.cpp文件中命名它们。当然,我从Java Wrapper中调用他们各自的名字,即test()。这是一个问题吗?以前经常使用JNI的人能否告诉我这是否是正确的方法?
。实际上我做了错误编译的头文件。你可以看看你看看头文件的函数名,例如:
JNIEXPORT jstring JNICALL Java_PTAPIWrapperForOmegaT_translateWithPTAPI
(JNIEnv *,jobject,jstring);
现在,看看你的Java文件包成员资格,在我的情况下:
package org.lingenio.util;
因为我以错误的方式编译头文件,JNI后来找不到符号因为它实际上是在寻找这个:
JNIEXPORT jstring JNICALL Java_org_lingenio_util_PTAPIWrapperForOmegaT_translateWithPTAPI(JNIEnv * env,jobject obj,jstring sentence )
所以,运气好的人在那里悬挂着同样的问题。我显然不是最伟大的Java程序员,这就是为什么我不得不担心这么长时间。我应该以正确的方式编译我的头文件首先。
检查你的包和类路径!
I've been looking for 2 days now and no solution could help me, so here we go again:
How to fix the UnsatisfiedLinkError... in JNI?
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");
}
}
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;
}
And the header file:
/* 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
and how I build it:
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
and finally, the error:
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)
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.
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.
EDIT:
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);
Now, take a look at your Java files' package membership, in my case:
package org.lingenio.util;
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)
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< Native方法>的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!