3 Replies Latest reply on Oct 10, 2002 9:08 AM by 843829

    Problem with the JNI under Linux

    843829
      When starting my JAVA program, which is using a shared library with C++ symbols, I am getting the following message:

      bley@Nibbler:~/VictoriaGUI> /usr/java/j2sdk1.4.1/bin/java Victoria/GUI
      Exception in thread "main" java.lang.UnsatisfiedLinkError:
      /home/bley/VictoriaGUI/libGUI.so: /home/bley/VictoriaGUI/libGUI.so:
      undefined symbol:
      __Q22bb6ObjectRCt12basic_string3ZcZt18string_char_traits1ZcZt24__default_alloc_template2b1i0
      at java.lang.ClassLoader$NativeLibrary.load(Native Method)
      at java.lang.ClassLoader.loadLibrary0(ClassLoader.java:1473)
      at java.lang.ClassLoader.loadLibrary(ClassLoader.java:1397)
      at java.lang.Runtime.loadLibrary0(Runtime.java:788)
      at java.lang.System.loadLibrary(System.java:832)
      at Victoria.GUI.<init>(GUI.java:67)
      at Victoria.GUI.main(GUI.java:126)

      I have no idea what the library might be missing and I am running out of ideas.
      Can anyone point me in the right direction, so that I can solve this problem?

      Florian
        • 1. Re: Problem with the JNI under Linux
          843829
          This is just a guess: You can't call C++ directly from java; you have to call C. Typically this is a matter of the linkage you have defined in your h and c files. Look back thorugh this forum and you will find many examples of specifying C linkage.
          • 2. Re: Problem with the JNI under Linux
            843829
            Thanks for the hint.

            The file that contains the functions that are called from JAVA is a C file, although one C++ object is defined and used there so that I can access my C++ classes. That's why I have to compile the file with g++. I wonder how I should call C++ methods from a pure C file since gcc won't be able to compile the references to the C++ classes.

            Is there anyone here who has used the JNI to interace JAVA with C++ and can tell me how to do it?

            Thanks
            Florian
            • 3. Re: Problem with the JNI under Linux
              843829
              Alright, just to clarify this for any future readers with a similar problem.

              The real problem that created the error output had been a missing linkage to stdc++ in my Makefile. I noticed this when doing a "ldd -r" on my shared object.

              The thing with calling C++ code can be done through using the Header file generated by "javah -jni", which puts an "extern C" around the function prototypes. If you're using pure C code you do not need to use the header file. It's just there to tell g++ to generate symbols without signature mapping. You obviously cannot use overloaded functions with the JNI.

              Now I've still got a "An unexpected exception has been detected in native code outside the VM", but I assume that this is some bug in my shared object.