JNI & access to member variables access issue

Discussion in 'Java' started by Ph. Barthelemy, Jan 4, 2005.

  1. 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
     
    Ph. Barthelemy, Jan 4, 2005
    #1
    1. Advertising

  2. Ph. Barthelemy

    Ann Guest

    "Ph. Barthelemy" <> wrote in message
    news:...
    > 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 ?
     
    Ann, Jan 4, 2005
    #2
    1. Advertising

  3. > 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...
     
    P. Barthelemy, Jan 4, 2005
    #3
  4. 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
     
    Gordon Beaton, Jan 5, 2005
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Alex Hunsley

    IBM's JNI fails where Sun's JNI works

    Alex Hunsley, Nov 3, 2003, in forum: Java
    Replies:
    4
    Views:
    851
    Alex Hunsley
    Nov 4, 2003
  2. Pasturel Jean-Louis

    Porting JNI Windows under JNI LINUX + Wine ?

    Pasturel Jean-Louis, Feb 29, 2004, in forum: Java
    Replies:
    5
    Views:
    907
    Pasturel Jean-Louis
    Mar 3, 2004
  3. vasanth
    Replies:
    0
    Views:
    2,702
    vasanth
    Jan 25, 2005
  4. vasanth
    Replies:
    0
    Views:
    623
    vasanth
    Jan 25, 2005
  5. Replies:
    9
    Views:
    961
Loading...

Share This Page