1

My java class has a static function defined as follows:

public static void logEvent(final String eventName, final String jObject) {
        //Function data
    }

Now my cpp file has the following function

void PingoScreen::callApslarIntegration(){
    char* eventName="bingo";
    JniMethodInfo t;  

if (JniHelper::getStaticMethodInfo(t, "com/myapp/test/ApslarSetup","logEvent", "()V")) {
    t.env->CallStaticVoidMethod(t.classID, t.methodID);
    t.env->DeleteLocalRef(t.classID);
  }
}

How can i send two string params to the JAVA function via JNI ?

Kind Regards

===============================================================

void PingoScreen::callApsIntegration() {

    JniMethodInfo t;

    if (JniHelper::getStaticMethodInfo(t, "com/myapp/test/ApslarSetup",
            "logJSONEvent", "()V")) {

        const char* cstr1 = "Test1";
        const char* cstr2 = "Test2";

        jstring jstr1 = t.env->NewStringUTF(cstr1);
        jstring jstr2 = t.env->NewStringUTF(cstr2);

        t.env->CallStaticVoidMethod(t.classID, t.methodID,jstr1,jstr2);
        t.env->DeleteLocalRef(t.classID);
    }
}

The above function causes a crash ?

==================================

Finally got it to work

void PingoScreen::callApslarIntegration() {
    JniMethodInfo t;
    if (JniHelper::getStaticMethodInfo
                (t, "com/nbs/test/ApslarSetup",
                "logJSONEvent",
                "(Ljava/lang/String;Ljava/lang/String;)V")) {

        const char* cstr1 = "Test1";
        const char* cstr2 = "Test2";

        jstring jstr1 = t.env->NewStringUTF(cstr1);
        jstring jstr2 = t.env->NewStringUTF(cstr2);

        t.env->CallStaticVoidMethod(t.classID, t.methodID,jstr1,jstr2);
        t.env->DeleteLocalRef(t.classID);
    }
}

The crash was due to the line

if (JniHelper::getStaticMethodInfo
                    (t, "com/nbs/test/ApslarSetup",
                    "logJSONEvent",
                    "(Ljava/lang/String;Ljava/lang/String;)V"))

The signiture had to be Ljava/lang/String;Ljava/lang/String; and not Ljava/lang/String;Ljava/lang/String (notice the last semi-colon)

1
  • 1
    Tip: Use javap -s -public com.nbs.test.ApslarSetup | egrep -A 2 "logJSONEvent" to obtain signatures to use with JNI. Commented Dec 15, 2013 at 5:05

1 Answer 1

3

If you want to use CallStaticVoidMethod, simply pass two jstrings to it, like so:

const char* cstr1 = "Test1";
const char* cstr2 = "Test2";
jstring jstr1 = t.env->NewStringUTF(cstr1);
jstring jstr2 = t.env->NewStringUTF(cstr2);
t.env->CallStaticVoidMethod(t.classID, t.methodID, jstr1, jstr2);

You can also use CallStaticVoidMethodA or CallStaticVoidMethodV instead of simply CallStaticVoidMethod. See the documentation here.

Sign up to request clarification or add additional context in comments.

4 Comments

I have edited my question with the updated function. Its causing a crash ?
Any error log? I'm not by a PC to test it. If the problem is in what I wrote, it's how I'm creating the jstrings, so you may find something quicker by Googling...
Just a C stack strace that does not seem discernible to me ?
@Dave- Thank you for your help. The problem was in my code. Thank you for your time.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.