java JNI 将参数传递给 C++ 的方法
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6653040/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
JNI pass parameters to method of c++
提问by user200340
I have a c++ file myCppTest.cpp which has method
我有一个 C++ 文件 myCppTest.cpp,它有方法
int myFunction(int argv, char **argc) { }
and a Java native method in myClass.java
和 myClass.java 中的 Java 本地方法
public native int myFunction (int argv, char[][] argc);
After generate the header file using javah -jni myClass, i have the header
使用 javah -jni myClass 生成头文件后,我有头文件
JNIEXPORT jint JNICALL Java_JPTokenizer_init (JNIEnv *, jobject, jint, jobjectArray);
In my myClass.cpp, I defined
在我的 myClass.cpp 中,我定义了
JNIEXPORT jint JNICALL Java_JPTokenizer_init (JNIEnv *env, jobject obj, jint argv, jobjectArray argc) { //need to call int myFunction(int argv, char **argc) in myCppTest.cpp }
How could I pass the arguments "jint argv, jobjectArray argc" to "int argv, char **argc", thanks.
我怎么能将参数“jint argv,jobjectArray argc”传递给“int argv,char **argc”,谢谢。
EDIT:
编辑:
I THINK I MADE A MISTAKE
我想我犯了一个错误
The Java native method in myClass.java should bepublic native int init (int argv, char[][] argc);
So there is
JNIEXPORT jint JNICALL Java_myClass_init (JNIEnv *, jobject, jint, jobjectArray);
generated after javah. And in myClass.cpp, i have
JNIEXPORT jint JNICALL Java_myClass_init (JNIEnv *env, jobject obj, jint argv, jobjectArray argc) { //need to call int myFunction(int argv, char **argc) in myCppTest.cpp }myClass.java 中的 Java 本地方法应该是
public native int init (int argv, char[][] argc);
所以有
JNIEXPORT jint JNICALL Java_myClass_init (JNIEnv *, jobject, jint, jobjectArray);
javah后生成。在 myClass.cpp 中,我有
JNIEXPORT jint JNICALL Java_myClass_init (JNIEnv *env, jobject obj, jint argv, jobjectArray argc) { //need to call int myFunction(int argv, char **argc) in myCppTest.cpp }
采纳答案by Tony the Pony
There is no direct mapping between Java objects and C++ primitives, so you will have to convert the arguments that are passed by the Java runtime environment, and then call your function.
Java 对象和 C++ 原语之间没有直接映射,因此您必须转换 Java 运行时环境传递的参数,然后调用您的函数。
Java will call Java_JPTokenizer_init
-- this is where you perform your conversion and invoke your "plain old" C++ function.
Java 将调用Java_JPTokenizer_init
——这是您执行转换并调用“普通”C++ 函数的地方。
To convert the array of strings, you will first need to access the array, then the individual strings.
要转换字符串数组,您首先需要访问数组,然后是单个字符串。
For array access, see http://java.sun.com/docs/books/jni/html/objtypes.html#5279.
For string access, see http://java.sun.com/docs/books/jni/html/objtypes.html#4001.
回答by Nick
You can create an object of the class and invoke method just like any other C++ code.
您可以像任何其他 C++ 代码一样创建类的对象并调用方法。
JNIEXPORT jint JNICALL Java_JPTokenizer_init
(JNIEnv *env, jobject obj, jint argv, jobjectArray argc) {
myClass obj; //create object of the class you want
obj.myFunction((int) argv, (char *) &argc); //call the method from that object
}