0

I want to call the Java method from Jni code with the int and int[] arguments. for that i have Go-ogled and found the following sample . Calling a java method from c++ in Android And it worked fine with String parameter . But While trying with int i got issues . Please help me .

JNI CODE:

jstring Java_com_calljavafromjni_MainActivity_getJniString( JNIEnv* env, jobject obj){

    jstring jstr = (*env)->NewStringUTF(env, "RAJESH TEST from JNI ");
    jint sss=1111;
    jclass clazz = (*env)->FindClass(env, "com/calljavafromjni/MainActivity");


    jmethodID messageMe = (*env)->GetMethodID(env, clazz, "messageMe", "(Ljava/lang/String;)Ljava/lang/Integer;");
    jobject result = (*env)->CallObjectMethod(env, obj, messageMe, sss);

   const char* str = (*env)->GetStringUTFChars(env,(jstring) result, NULL); // should be released but what a heck, it's a tutorial :)
    printf("%s\n", str);

   return (*env)->NewStringUTF(env, str);


}

Java code

public String messageMe(Integer text) {
    System.out.println( "aaaaaaaaaaaaaaaa "+text);

    return "test";
}
Community
  • 1
  • 1
RAJESH
  • 231
  • 1
  • 5
  • 19
  • Could you give an example of the Java method you want to call, and how you are trying to call it? The [JNI Specification](http://docs.oracle.com/javase/6/docs/technotes/guides/jni/spec/jniTOC.html) really does contain everything you need to know, its just not always easy to work out what you need. – n.collins Nov 21 '12 at 12:29
  • i have updated java method in update . – RAJESH Nov 21 '12 at 13:59
  • And you expect the output to be `aaaaaaaaaaaaaaaa 1111` ? Where do int[] come in? – n.collins Nov 21 '12 at 14:03
  • Let us know if you managed to solve the problem. – n.collins Nov 26 '12 at 11:57

1 Answers1

3

I don't see where int[] come into your problem, but with int it should be easy to solve.

You need to look at your GetMethodId() call, specifically the method signature argument (the last one). The JNI Specification provides a list of all its Type Signatures here. That should also help you when you eventually come to pass your int arrays too.

So we can see at the moment your signature is:

String messageMe(Integer text)

but you told JNI it was (Ljava/lang/String;)Ljava/lang/Integer; which translates to something like:

java.lang.Integer messageMe(String text)

The Type Signatures show us that the signature for an int is simply I so your argument for GetMethodId() should be something like this:

jmethodID messageMe = (*env)->GetMethodID(env, clazz, "messageMe", "(I)Ljava/lang/String;");

I hope that helps. As I said before, JNI isn't the easiest thing to get into but the answers really are all in the Specification, you just have to look quite hard.

EDIT: I corrected my signature.

Basically, you were almost there - you just got the arguments and return value the wrong way around in the signature. It should be (<arguments>)<return type>. You also made the easy mistake of specifying the class for Integer, instead of the primitive type.

n.collins
  • 343
  • 1
  • 9