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]

Does jni work?


Hi!

I tried to get a jni test working. I started by the example on the web and
adjusted to compile with gcj (newest snapshot 2001-01-22 on linux)

These are the files:

===> Makefile <===
all: sample

%: %.java
	gcj -o $@ --main=$(basename $<) $<

libsample.so: sampNat.c sample.h
	gcc -shared -fPIC -o $@ $<

sample.class: sample.java
	gcj -C $< 

sample.h:  sample.class
	gcjh -jni sample

sample: libsample.so sample.java
	 gcj -fjni -o $@ --main=sample -L. -lsample sample.java

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


===> sampNat.c <===
#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);
}

===> 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");
  }
}

the program links successfully, but bombs at execution time with
Exception in thread "main" java.lang.UnsatisfiedLinkError: sampNat: file not found
   at 0x401984e9: java::lang::Throwable::Throwable(java::lang::String*) (/sw/snapshots/lib/libgcj.so.1)
   at 0x4018caf0: java::lang::Error::Error(java::lang::String*) (/sw/snapshots/lib/libgcj.so.1)
   at 0x4018ec40: java::lang::LinkageError::LinkageError(java::lang::String*) (/sw/snapshots/lib/libgcj.so.1)
   at 0x40198980: java::lang::UnsatisfiedLinkError::UnsatisfiedLinkError(java::lang::String*) (/sw/snapshots/lib/libgcj.so.1)
   at 0x4017cd1d: java::lang::Runtime::_load(java::lang::String*, bool) (/sw/snapshots/lib/libgcj.so.1)
   at 0x40190c66: java::lang::Runtime::loadLibrary(java::lang::String*) (/sw/snapshots/lib/libgcj.so.1)
   at 0x4019514e: java::lang::System::loadLibrary(java::lang::String*) (/sw/snapshots/lib/libgcj.so.1)
   at 0x0804bdc4: _ZN6sample_003cclinit_003eEvU (/home/kahlert/LOCAL/Hello/sample.java:17)
   at 0x40178b73: java::lang::Class::initializeClass() (/sw/snapshots/lib/libgcj.so.1)
   at 0x40175cd9: gnu::gcj::runtime::FirstThread::run() (/sw/snapshots/lib/libgcj.so.1)
   at 0x4018081b: java::lang::Thread::run_(java::lang::Object*) (/sw/snapshots/lib/libgcj.so.1)
   at 0x4028e405: _Jv_ThreadSetPriority(_Jv_Thread_t*, int) (/sw/snapshots/lib/libgcj.so.1)
   at 0x4042d6cc: GC_start_routine (/sw/snapshots/lib/libgcjgc.so.1)
   at 0x40445e93: pthread_detach (/lib/libpthread.so.0)
   at 0x4050565a: __clone (/lib/libc.so.6)

Did i make any mistake, or is this known not to work?

Thanks,
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]