Hi,

I tried your example code and it did not seg-fault.  I received an
unsatisfied link error.  Upon closer examination, the test.h file generated
by javah did not seem to be correct (i.e. it did not match your declaration
or parameter list in hello.C

Using nm on libhello.so showed this to be the case (some C++ name mangling?)

I simply copied the declaration from hello.C and replaced the appropriate
part of test.h with the correct (i.e. matching) declaration

Then my unsatified link error went away and everything worked fine.

There seems to be some trouble with javah and C++.  I am not sure exactly
what is up.

Anyway, here are my slightly modified pieces of code:

The java code:

class Test
{
   public native void display();

   static { System.loadLibrary("hello"); }

   public static void main(String[] args)
   {
      Test t = new Test();
      t.display();
   }
}


The C++ code:

/* hello.C */
#include <jni.h>
#include "Test.h"
#include <iostream.h>

JNIEXPORT void JNICALL
Java_Test_display(JNIEnv *env, jobject obj)
{
 cout << "Hello world!\n" << endl;
 return;
}

/*
[user@ravel native]$ g++ -Wall -shared -fPIC
-I/usr/local/jdk1.1.6/include -I/usr/local/jdk1.1.6/include/genunix -o
libhello.so hello.C
---
[user@ravel native]$ LD_LIBRARY_PATH=.:$LD_LIBRARY_PATH java test > err
2>&1
*/


And finally the "fixed" Test.h header file which you can compare to the one
generated by javah.

/* DO NOT EDIT THIS FILE - it is machine generated */
#include <native.h>
/* Header for class Test */

#ifndef _Included_Test
#define _Included_Test

#pragma pack(4)

typedef struct ClassTest {
    char PAD;   /* ANSI C requires structures to have a least one member */
} ClassTest;
HandleTo(Test);

#pragma pack()

#ifdef __cplusplus
extern "C" {
#endif

JNIEXPORT void JNICALL Java_Test_display(JNIEnv *env, jobject obj);

#ifdef __cplusplus
}
#endif
#endif


I hope this helps.

I don't know why this is happening.  If this solution makes "no sense" to
you please submit an official bug report to the Blackdown Jitterbug bug
database.

Kevin
Blackdown JDK Poriting Team






----------------------------------------------------------
Kevin B. Hendricks
Associate Professor, Operations & Information Technology
School of Business, College of William & Mary
Williamsburg, VA 23187, [EMAIL PROTECTED]
http://business.tyler.wm.edu

Reply via email to