Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > JNI & access to member variables access issue

Reply
Thread Tools

JNI & access to member variables access issue

 
 
Ph. Barthelemy
Guest
Posts: n/a
 
      01-04-2005
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
 
Reply With Quote
 
 
 
 
Ann
Guest
Posts: n/a
 
      01-04-2005

"Ph. Barthelemy" <> wrote in message
news: om...
> 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 ?


 
Reply With Quote
 
 
 
 
P. Barthelemy
Guest
Posts: n/a
 
      01-04-2005
> 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...

 
Reply With Quote
 
Gordon Beaton
Guest
Posts: n/a
 
      01-05-2005
On 4 Jan 2005 14:41:57 -0800, Ph. Barthelemy wrote:
> 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

--
[ do not email me copies of your followups ]
g o r d o n + n e w s @ b a l d e r 1 3 . s e
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Put variables into member variables or function variables? tjumail@gmail.com C++ 9 03-23-2008 04:03 PM
Re: JNI: Error loading DLL from JNI DDL vasanth Java 0 01-25-2005 11:01 AM
Porting JNI Windows under JNI LINUX + Wine ? Pasturel Jean-Louis Java 5 03-03-2004 07:50 PM
How would I use qsort to sort a struct with a char* member and a long member - I want to sort in order of the long member Angus Comber C Programming 7 02-05-2004 06:41 PM
IBM's JNI fails where Sun's JNI works Alex Hunsley Java 4 11-04-2003 10:34 AM



Advertisments