Last active
November 8, 2022 17:44
-
-
Save mageddo/6e8b394da8657d2026ad922359b3e42e to your computer and use it in GitHub Desktop.
JNI call static method with arguments and return object
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
JNIEnv *jni; | |
// class and method finding | |
jclass clazz = jni->FindClass("java/lang/String"); | |
jmethodID method = jni->GetStaticMethodID(clazz, "valueOf", "(Ljava/lang/Object;)Ljava/lang/String;"); | |
if(method == NULL){ | |
jni->FatalError("Class method not found\n"); | |
} | |
// method execution | |
jstring arg1 = jni->NewStringUTF("Hello World!!!"); | |
jstring result = (jstring) jni->CallStaticObjectMethod(clazz, method, arg1); | |
jni->ReleaseStringUTFChars(arg1, NULL); | |
if (result == NULL) { | |
jni->FatalError("couldn't call method"); | |
} | |
// result presentation | |
jboolean isCopy = (jboolean) false; | |
printf("result: %s\n", jni->GetStringUTFChars(result, &isCopy)); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
#gg