[gcj+cni] Segmentation fault when accessing java code from c++

Christian Nolte ch.nolte@noltec.org
Wed Apr 23 20:47:00 GMT 2008


David Daney schrieb:
> Christian Nolte wrote:
> 
>>
>> int main() {
>>   Test t;
>>   t.test2();
>> }
>> - ---
>>
> .
> .
> .
>>
>> Am I doing something wrong, i.e. do I miss some preinitialization which
>> is needed to get CNI working? Or is this a bug in my libgcj?
>>
> 
> In short yes.  If you are calling from C++ into CNI code, you must use
> the invocation interface (much as you must use the JIN invocation
> interface when using JIN).
> 
> See: http://gcc.gnu.org/onlinedocs/gcc-4.3.0/gcj/Invocation.html

Thank you very much for that hint! That did the trick.

Just for the record, this is what I did:

--- Test.cpp
#include <gcj/cni.h>
#include <java/lang/System.h>
#include <java/io/PrintStream.h>
#include <java/lang/Throwable.h>

#include "Test.h"

int main() {
  using namespace java::lang;

  try
  {
    JvCreateJavaVM(NULL);
    JvAttachCurrentThread(NULL, NULL);

    JvInitClass(&Test::class$);

    Test t;
    t.test2();
    Test::test();


    JvDetachCurrentThread();
  }
  catch (Throwable *t)
  {
    System::err->println(JvNewStringLatin1(
	"Unhandled Java exception:"));
    t->printStackTrace();
  }
}
---

	g++ -c Test.cpp

and

	gcj Test.o -ltest -L. -o testprog

then

[nolte@ravine java-test]$ ./testprog
test2() called
test() called

-- 
   For more than 4 generations the IT Professionals were the
   guardians of quality and stability in software. Before the
   dark times.	Before Microsoft...



More information about the Java mailing list