This is the mail archive of the java-discuss@sources.redhat.com mailing list for the Java project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]

Re: Does jni work?


Hi,
it's me again!

There seem to be more . versus / issues lurking, here.

The following works:

#include <jni.h>
#include "sample.h"
#include <stdio.h>

void 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);
}

According to 
http://java.sun.com/docs/books/tutorial/native1.1/implementing/field.html
it should read for example
fid = (*env)->GetStaticFieldID(env, cls, "out", "Ljava/io/PrintStream;");

I would correct these issues, if anybody gave me a hint where the
functions GetStaticFieldID... are implemented.

At least i have workaround,

Thanks for all your great work,
Martin.


-- 
The early bird gets the worm. If you want something else for       
breakfast, get up later.

Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]