JNI findClass does't run properly
Hello,
I'm new with jni ... i'm tryng to access a java class from inside a c native code in order to map such class to a c struct.
More in depth, for example, I have a java class with 2 fields of type int (final int id=1, final int value=2) ... once I load such class inside the native code and print the values I obtain 0 for both with no errors.
Heare is the java code:
jniStruct.java
package jniStruct;
publicclass jniStruct{
privatenativeint ccode();
static{
System.loadLibrary("cJavaInterface");
}
publicstaticvoid main(String[] args){
jniStruct jnistruct =new jniStruct();
Js s =new Js();
int retCode = jnistruct.ccode();
}
}
the java class Js.java
package jniStruct;
publicclass Js{
finalint id=1;
finalint value=2;
}
and finally the c code
#include"jni.h"
#include"jniStruct_jniStruct.h"
JNIEXPORT jint JNICALL Java_jniStruct_jniStruct_ccode (JNIEnv *env, jobject obj){
int l_id;
long l_value;
jclass cls = (*env)->FindClass(env,"jniStruct/Js");
jfieldID idID = (*env)->GetFieldID(env, cls,"id","I");
jfieldID valueID = (*env)->GetFieldID(env,cls,"value","I");
l_id = (*env)->GetIntField(env, cls ,idID);
l_value = (*env)->GetIntField(env, cls ,valueID);
printf(" %d %d\n",l_id,l_value);
}
with the output:
$ 0 0
any suggestion? thank you
Stefano

