JNI: cannot access member of superclass via subclass

Kees Huizing keesh at win.tue.nl
Tue May 25 05:32:54 PDT 1999

I want to access a member field of a Java object from within C code, using JNI.
When this member field is declared in a superclass of the object, I get an 
error. With the JDK, it works as expected. (It also goes wrong with a 
non-private field.)

When I run the following code, I get

% kaffe bar
x is now 0
        at bar.main(7)

I use Kaffe 1.00 on Linux RedHat 5.2

When I do this on Solaris 2.5.1 with JDK 1.2, I get

% java bar
x is now 6

I can circumvent this problem by using in bar.c

klass = (env*)->FindClass(env, "foo");

but this is awkward.

Is this a known bug?

Kees Huizing

-- foo.java --
public class foo
   private int x = 6;

-- bar.java --
public class bar extends foo
   public native void print_x();

   public static void main( String[] args )
      new bar().print_x();

   static {
      System.loadLibrary( "bar" );

-- bar.c --
#include "bar.h"
#include <stdio.h>

JNIEXPORT void JNICALL Java_bar_print_1x(JNIEnv *env, jobject obj)
        jint x;
        jclass klass = (*env)->GetObjectClass(env, obj);
        jfieldID fid_x = (*env)->GetFieldID(env, klass, "x", "I");
        x = (*env)->GetIntField(env, obj, fid_x);

        printf("x is now %d\n", x);

Kees Huizing - keesh at win.tue.nl - Dept. of Mathematics and Computer Science 
Eindhoven University of Technology - The Netherlands * FREE SPEECH ON-LINE
tel:+31-40-2474120 -  fax:+31-40-2468505 - http://www.win.tue.nl/~keesh

More information about the kaffe mailing list