JNI pass parameters to method of c++

I have a c++ file myCppTest.cpp which has method

int myFunction(int argv, char **argc) {
}

and
a Java native method in myClass.java

public native int myFunction (int argv, char[][] argc);

After generate the header file using javah -jni myClass, i have the header

JNIEXPORT jint JNICALL Java_JPTokenizer_init
  (JNIEnv *, jobject, jint, jobjectArray);

In my myClass.cpp, I defined

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.

EDIT:

I THINK I MADE A MISTAKE

The Java native method in myClass.java should be

public 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 
}

Answer

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 will call Java_JPTokenizer_init — this is where you perform your conversion and invoke your “plain old” C++ function.

To convert the array of strings, you will first need to access the array, then the individual strings.

Leave a Reply

Your email address will not be published. Required fields are marked *