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

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

Hash: SHA1


I have a simple example here which I cannot get to run correctly. I want
 to compile Java-Code to a native shared object and access this code
from C++. I have the following code:

- --- Test.java
public class Test {

  public static void main(String[] args) {
    try {
    } catch (Exception e) {
      System.out.println("no options provided");

  public static void test() {
    System.out.println("test() called");

  public void test2() {
   System.out.println("test2() called");
- ---

I compile this to the so 'libtest.so' in the following manner:

	gcj Test.java -lgij -o libtest.so -shared -fPIC

I then generate the appropriate C++ header like this:

	gcjh --classpath=. Test

Then I compile the following C++-Code to the binary 'testprog':

- --- Test.cpp
#include <gcj/cni.h>

#include "Test.h"

int main() {
  Test t;
- ---

	g++ Test.cpp -ltest -L.

This all works fine and without any problem. When I run the app I get a
segmentation fault:

[nolte@ravine java-test]$ export LD_LIBRARY_PATH=`pwd`
[nolte@ravine java-test]$ ./testprog
Segmentation fault

Valgrind shows many problems in /usr/lib/libgcj.so.8rh.0.0 (Use of
uninitialised value). The gdb backtrace is:

- ---
(gdb) bt
#0  0x039fc5b1 in _Jv_equalUtf8Consts () from /usr/lib/libgcj.so.8rh
#1  0x03a48b67 in _Jv_GetMethodLocal () from /usr/lib/libgcj.so.8rh
#2  0x03a4b024 in java::lang::Class::initializeClass () from
#3  0x03a4b069 in java::lang::Class::initializeClass () from
#4  0x03a4c626 in _Jv_InitClass () from /usr/lib/libgcj.so.8rh
#5  0x00111bd6 in Test.test2()void () from
#6  0x080485cb in main ()
- ---

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?

Best regards,

- --
   For more than 4 generations the IT Professionals were the
   guardians of quality and stability in software. Before the
   dark times.	Before Microsoft...
Version: GnuPG v1.4.7 (GNU/Linux)
Comment: Using GnuPG with Fedora - http://enigmail.mozdev.org


More information about the Java mailing list