JNI & access to member variables access issue

P

Ph. Barthelemy

Hi,

I am trying to get and set a boolean member variable ( in a Java class
) from the C-side code.
Pretty basic, eh...

but it does not work :
- a C-side 'get', always returns 0,
- a C-side 'set' does not change the value on the Java-side, but if I
'get' the value on the C-side, the 'set' seems to work...

Of course, the variable env, obj and the jfieldID's are not null...

I am clueless...
Any idea anyone ?

the code snippet follows :
_______________________________________
__ the java variable declaration
private boolean useBulkRead = false;

_______________________________________
__ THE SNIPPET THAT CALLS THE 2 FUNCTIONS
jboolean j = getUseBulkReadField(env, obj );
fprintf(stderr,"HeartBit:: getUseBulkReadField( : %d... \n", j);

setUseBulkReadField(env, obj,(jboolean) 1 );
j = getUseBulkReadField(env, obj );
fprintf(stderr,"HeartBit:: getUseBulkReadField( : %d... \n", j);
fflush( stderr );

_______________________________________
__ read a boolean member variable
jboolean getUseBulkReadField(JNIEnv *env, jobject obj) {
jclass cls = (*env)->GetObjectClass(env, obj);
jfieldID fidUseBulkRead;
fidUseBulkRead = (*env)->GetFieldID( env, cls,
"useBulkRead","Z" );
return (*env)->GetBooleanField(env, cls, fidUseBulkRead );

}
________________________________________
__ set this variable
jboolean setUseBulkReadField(JNIEnv *env, jobject obj, jboolean value)
{
jclass cls = (*env)->GetObjectClass(env, obj);
jfieldID fidUseBulkRead;
fidUseBulkRead = (*env)->GetFieldID( env, cls,
"useBulkRead","Z" );
(*env)->SetBooleanField(env, cls, fidUseBulkRead, value );
}


Thanks for helping !

--Philippe
 
A

Ann

Ph. Barthelemy said:
Hi,

I am trying to get and set a boolean member variable ( in a Java class
) from the C-side code.
Pretty basic, eh...

but it does not work :
- a C-side 'get', always returns 0,
- a C-side 'set' does not change the value on the Java-side, but if I
'get' the value on the C-side, the 'set' seems to work...

Of course, the variable env, obj and the jfieldID's are not null...

Just asking: does "jdb" debugger work for JNI ?
 
P

P. Barthelemy

Just asking: does "jdb" debugger work for JNI ?

Hmmm... I am using Eclipse ( and I do not know what's behind its debugger )
Of course, I am debugging interactively the java-side, and debugging the
C-side with The good old printf...
 
G

Gordon Beaton

I am trying to get and set a boolean member variable ( in a Java
class ) from the C-side code. Pretty basic, eh...

but it does not work :
- a C-side 'get', always returns 0,
[...]

__ read a boolean member variable
jboolean getUseBulkReadField(JNIEnv *env, jobject obj) {
jclass cls = (*env)->GetObjectClass(env, obj);
jfieldID fidUseBulkRead;
fidUseBulkRead = (*env)->GetFieldID( env, cls,
"useBulkRead","Z" );
return (*env)->GetBooleanField(env, cls, fidUseBulkRead );

Your error is the last quoted line above: look up the field value in
the object itself, not its class (and similarly when setting the value
in the other corresponding method).

I would have expected that line to cause an exception. In your native
code, I'd recommend the use of the following construction when calls
to JNI functions fail:

if ((*env)->ExceptionOccurred(env)) {
(*env)->ExceptionDescribe(env);
}

/gordon
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,902
Latest member
Elena68X5

Latest Threads

Top