Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Creating an Integer Object in JNI

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!

like image 812
XTT Avatar asked Nov 14 '16 19:11

XTT


People also ask

What is Jclass in JNI?

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 {}; ...

What is JNIEnv * env?

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.

What is JNI CPP?

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.


1 Answers

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);
like image 130
Guilherme Campos Hazan Avatar answered Oct 30 '22 03:10

Guilherme Campos Hazan