Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JNI. How to get jstring from jobject and convert it to char*

This is what I have so far: I pass an object which has 2 fields: String and Integer, as a parameter and I want to send information to process it in C part, which is not important at this point... I get complains at jstring declaration

JNIEXPORT jint JNICALL Java_Tier3_NativeMethods_totalPalletsIn(
            JNIEnv *env, jclass cls, jobject stat) {

jclass staticsitcs = (*env)->GetObjectClass(env, stat);

// Here I try to get it using the ID
jfieldID idDate = (*env)->GetFieldID(env, staticsitcs, "date", "S");

jstring dateString = (jstring)(*env)->GetStringRegion(env, stat, idDate);

// Here converting whatever I get in jstring to char*
char* date = (*env)->GetStringUTFChars(env,dateString,0);

// Getting the 2nd field from the object
jfieldID idNumber = (*env)->GetFieldID(env, staticsitcs, "amount", "I");

jint amount = (*env)->GetDoubleField(env, stat, idNumber);

// Calling C method
jint totalPallets = checkTotalPalletsIn(date, amount);

(*env)->ReleaseStringUTFChars(env, dateString, date);

return totalPallets;
}

What am I missing?

like image 580
Aleksandr Avatar asked Dec 10 '13 17:12

Aleksandr


1 Answers

jstring dateString = (jstring)(*env)->GetObjectField(env, stat, idDate);

… and after that everything is OK.

like image 118
Alex Cohn Avatar answered Sep 21 '22 18:09

Alex Cohn