8

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?

Aleksandr
  • 91
  • 1
  • 2
  • 7

1 Answers1

13

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

… and after that everything is OK.

Alex Cohn
  • 56,089
  • 9
  • 113
  • 307
  • Than helped, but now I get "initialization discards «const» qualifier from pointer target type" when I try to pass the date into C method. Any tips on this? – Aleksandr Dec 10 '13 at 19:09
  • Did you by any chance declare **date** as const char *? – Alex Cohn Dec 10 '13 at 19:36
  • No, It's just char*. Well.. I figured out that it was just a warning, so nothing major, but now when I compile it I see this: /cygdrive/c/Temp/ccLeMJG9.s: Assembler messages: /cygdrive/c/Temp/ccLeMJG9.s:15: Error: invalid instruction suffix for `push' /cygdrive/c/Temp/ccLeMJG9.s:25: Error: operand type mismatch for `call' .... – Aleksandr Dec 11 '13 at 10:48
  • Ok, it's fixed now. Used 64bit compiler instead of 32 – Aleksandr Dec 11 '13 at 12:13
  • @AlexCohn can you please tell how to extract the class name from the Jclass object? – kumarD Feb 13 '17 at 06:14
  • @AlexCohn i am doing this ot get the method name (*jvmti)->GetMethodName(jvmti,frames[i].method,&methodName,NULL,NULL); and getting the jclass reference via : (*jvmti)->GetMethodDeclaringClass(jvmti,frames[i].method,&declaring_class_ptr); need help in getting the class name from jclass reference – kumarD Feb 13 '17 at 06:16