what happend to expanded gcj faq?

Glenn Chambers GChambers@provsol.com
Wed Dec 22 08:50:00 GMT 1999


> Some time ago I remember helping answer various questions for
> an expanded gcj/libgcj faq, such as why we use cni instead of jni.
> Well, now I can't find it on the gcj home page.  What happened?
> Where is it?

It seems to be up now.  Could use a little proof-reading; I'll
submit patches if I can get some free time.

Minor priority nit:  The CNI examples listed at the end as
provided by Tom Tromey are actually mine.  (If I actually
cared, I'd have put my name in them.)  In fact, they're
identical to the ones quoted inside the mail message pointed
to by the second link.

There's also a typo (mine) in the second example (cni-2.txt);
it actually reflects the state as of 2.96, not 2.95 as indicated.

Finally, if anyone thinks it would help, feel free to add the
following, which is my CNI example ported to JNI.  Might help
explain the benefits of CNI vs. JNI.

==> Makefile <==
libsampNat.so: sampNat.o
	gcc -shared -o libsampNat.so sampNat.o

sampNat.o: sampNat.c sample.h
	gcc -I/usr/include/kaffe -c sampNat.c

sample.h:  sample.class
	kaffeh -jni sample

sample.class: sample.java
	javac sample.java

clean:
	rm -f sample.h sample.class sampNat.o libsampNat.so

==> sampNat.c <==
#include <jni.h>
#include "sample.h"
#include <stdio.h>

JNIEXPORT void JNICALL Java_sample_myNative(JNIEnv* env, jobject this, 
                                            jstring s)
{
  jclass cls;
  jfieldID fid;
  jobject obj;
  jmethodID mid;

  printf("From C\n");

  cls = (*env)->FindClass(env, "java/lang/System");
  if (cls == 0) {
    printf("java/lang/System lookup failed\n");
    return;
  }
  fid = (*env)->GetStaticFieldID(env, cls, "out", "Ljava/io/PrintStream;");
  if (fid == 0) {
    printf("java/lang/System::out lookup failed\n");
    return;
  }
  obj = (*env)->GetStaticObjectField(env, cls, fid);
  if (obj == 0) {
    printf("GetStaticObjectField call failed\n");
    return;
  }
  cls = (*env)->GetObjectClass(env, obj);
  if (cls == 0) {
    printf("GetObjectClass(out) failed\n");
    return;
  }
  mid = (*env)->GetMethodID(env, cls, "println", "(Ljava/lang/String;)V");
  if (mid == 0) {
    printf("println method lookup failed\n");
    return;
  }
  (*env)->CallVoidMethod(env, obj, mid, s);
}

==> sample.java <==
public class sample {
  public native void myNative(String s);

  public void myJava(String s) {
    s = s + ", Java";
    System.out.println(s);
  }

  public static void main(String args[]) {
    sample x = new sample();
    x.myJava("Hello");
    x.myNative("Hello, Java (from C)");
    x.myJava("Goodbye");
  }
  
  static {
    System.loadLibrary("sampNat");
  }
}


More information about the Java mailing list