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

[2751 byte] By [Stefano_unilea] at [2007-10-3 10:33:17]
# 1
> > any suggestion?1. Always, always do error checking. Return values must be checked and where appropriate exception throws must be checked.2. Read the api - GetIntField takes an object not a class. (And error checking would probably expose that.)
jschella at 2007-7-15 5:56:24 > top of Java-index,Java HotSpot Virtual Machine,Specifications...
# 2

I always do error checking !! ... probably I've not explained clearly that the code i've posted doesn't produce errors.

I've successfuly compiled the library from c code and compiled and run the java code I've posted ... the problem is that the program returns values that doesn't correspond to values defined in the Js class

thank you

Stefano_unilea at 2007-7-15 5:56:24 > top of Java-index,Java HotSpot Virtual Machine,Specifications...
# 3
> I always do error checking !! The code you posted does not have error checking.The first three JNI methods in your C code need return value checks and the subsequent java exception checks.The last two methods need java exception checks.
jschella at 2007-7-15 5:56:24 > top of Java-index,Java HotSpot Virtual Machine,Specifications...
# 4

Sorry, I've misunderstood the meaning of words "error checking" ... I thought that possible errors in C code would have risen during compilation ... I simple didn' read the chapter about exceptions.

Now the code works!

[...]

JNIEXPORT jint JNICALL Java_jniStruct_jniStruct_ccode (JNIEnv *env, jobject obj) {

jint l_id;

jint l_value;

jclass cls = (*env)->FindClass(env,"jniStruct/Js");

jmethodID clsID = (*env)->GetMethodID(env, cls, "<init>", "()V");

jobject obj = (*env)->NewObject(env, cls, clsID);

jfieldID idID = (*env)->GetFieldID(env, cls, "id", "I");

jfieldID valueID = (*env)->GetFieldID(env,cls, "value", "I");

l_id = (*env)->GetIntField(env, obj, idID);

l_value = (*env)->GetIntField(env, obj ,valueID);

printf(" %d %d\n",l_id,l_value);

}

Thank you

Stefano

Stefano_unilea at 2007-7-15 5:56:24 > top of Java-index,Java HotSpot Virtual Machine,Specifications...