I want an Integer object to be return to the java layer from the JNI. The following code crashes when calling NewObject(env, interger, init, rc)
. What is the proper way to create a Integer object and return it?
jint rc = 0;
jclass intClass = (*env)->FindClass(env, "java/lang/Integer");
if (intClass == NULL) {
return NULL;
}
jmethodID init = (*env)->GetMethodID(env, intClass, "intValue", "()I");
if (init == NULL) {
return NULL;
}
jobject rc_obj = (*env)->NewObject(env, intClass, init, rc);
if (rc_obj == NULL) {
return NULL;
}
return rc_obj;
Thanks!
typedef jobject jclass; In C++, JNI introduces a set of dummy classes to enforce the subtyping relationship. For example: class _jobject {}; class _jclass : public _jobject {}; ...
jobject NewGlobalRef(JNIEnv *env, jobject obj); Creates a new global reference to the object referred to by the obj argument. The obj argument may be a global or local reference.
JNI Programming in C++ The JNI provides a slightly cleaner interface for C++ programmers. The jni. h file contains a set of inline C++ functions.
Try this:
jclass cls = (*env)->FindClass(env, "java/lang/Integer");
jmethodID midInit = (*env)->GetMethodID(env, cls, "<init>", "(I)V");
if (NULL == midInit) return NULL;
jobject newObj = (*env)->NewObject(env, cls, midInit, number);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With