8 Replies Latest reply: Jun 2, 2011 10:44 AM by 800456 RSS

    [JNI: Java to C Connectivity] .so file creation from g++

    Ahsan Asghar
      Hello Everyone,
      I am trying to run a simple JNI based example taken from JNI's specifications. I am running it on a Fedora OS based server. All the steps go fine but when I try to make a .so file from the C code, I get an error.

      I took the compilation command from the JNI specifications again. That command is for Solaris OS: cc -G -I/java/include -I/java/include/solaris
      HelloWorld.c -o libHelloWorld.so

      I have updated the command just to replace cc with g++ because I have g++ installed on the server.

      The error is:
      =============================================================
      -bash-3.1# g++ -G -I/opt/jdk1.6.0_24/include -I/opt/jdk1.6.0_24/include/linux HelloWorld.c -o libHelloWorld.so
      HelloWorld.c:10:2: warning: no newline at end of file
      /usr/lib/gcc/i386-redhat-linux/4.1.1/../../../crt1.o: In function `_start':
      (.text+0x18): undefined reference to `main'
      collect2: ld returned 1 exit status
      =============================================================

      Would you please guide how I can successfully compile the C code and make a .so file to be able to call C code from Java.

      Thanks & Regards,
      Ahsan
        • 1. Re: [JNI: Java to C Connectivity] .so file creation from g++
          865324
          this is not a java-question, but try
          g++ -shared
          • 2. Re: [JNI: Java to C Connectivity] .so file creation from g++
            Ahsan Asghar
            Thanks a lot for the help. I replaced -G flag with -shared flag but I kept the -I flag as it is. Now I am getting runtime error (details below), would you please help to resolve it.

            I have got the following error/result of the command this time:
            ===============================================
            g++ -shared -I/opt/jdk1.6.0_24/include -I/opt/jdk1.6.0_24/include/linux HelloWorld.c -o libHelloWorld.so
            HelloWorld.c:10:2: warning: no newline at end of file
            ===============================================

            Although the .so file was created successfully this time but when I ran the program with ./java HelloWorld command, I have got a runtime error on it:
            ===============================================
            ./java HelloWorld
            Exception in thread "main" java.lang.UnsatisfiedLinkError: no HelloWorld in java.library.path
            at java.lang.ClassLoader.loadLibrary(ClassLoader.java:1734)
            at java.lang.Runtime.loadLibrary0(Runtime.java:823)
            at java.lang.System.loadLibrary(System.java:1028)
            at HelloWorld.<clinit>(HelloWorld.java:8)
            Could not find the main class: HelloWorld. Program will exit.
            ===============================================

            Thanks a lot for the help,
            Ahsan
            • 3. Re: [JNI: Java to C Connectivity] .so file creation from g++
              EJP
              You're not finding your native library. You need HelloWorld.dll or libHelloWorld.so to be in the current directory, or the PATH, or the java.library.path, when you execute that command. I don't see how that can be the case if ./java works, as java is installed in a 'bin' directory in the JRE or JDK. Unless you are polluting the JRE/JDK with your own source and object code, which you shouldn't be doing either.
              • 4. Re: [JNI: Java to C Connectivity] .so file creation from g++
                Ahsan Asghar
                I am not sure about the java.library.path thing, should I get help to fix that?

                Otherwise, I have put all the files related to this program (HelloWorld.c, HelloWorld.class, HelloWorld.h, HelloWorld.java and libHelloWorld.so) in the bin folder of JDK (/opt/jdk1.6.0_24/bin).

                Should I put my code/files elsewhere and how will that effect the path requirement?

                Thanks,
                Ahsan
                • 5. Re: [JNI: Java to C Connectivity] .so file creation from g++
                  EJP
                  I've just answered that. You're only reiterating what I just told you.
                  • 6. Re: [JNI: Java to C Connectivity] .so file creation from g++
                    800456
                    don't put ANY of your files inn the java BIN directory.

                    Put them in your own directory, and complie them.

                    then run java, specifying the path to the .so file:

                    java -Djava.library.path="(absolute) path to your .so file" "your main class file"
                    • 7. Re: [JNI: Java to C Connectivity] .so file creation from g++
                      Ahsan Asghar
                      Dear Champions!
                      I tried the command that is given in post above. I actually tried the path with file name (java -Djava.library.path=/opt/jtestcode/libHelloWorld.so HelloWorld) and without file name (java -Djava.library.path=/opt/jtestcode/ HelloWorld). Both these times, I got the same error:
                      ==========================================
                      java -Djava.library.path=/opt/jtestcode/libHelloWorld.so HelloWorld
                      Exception in thread "main" java.lang.ClassFormatError: HelloWorld (unrecognized class file version)
                      at java.lang.VMClassLoader.defineClass(libgcj.so.7rh)
                      at java.lang.ClassLoader.defineClass(libgcj.so.7rh)
                      at java.security.SecureClassLoader.defineClass(libgcj.so.7rh)
                      at java.net.URLClassLoader.findClass(libgcj.so.7rh)
                      at gnu.gcj.runtime.SystemClassLoader.findClass(libgcj.so.7rh)
                      at java.lang.ClassLoader.loadClass(libgcj.so.7rh)
                      at java.lang.ClassLoader.loadClass(libgcj.so.7rh)
                      at gnu.java.lang.MainThread.run(libgcj.so.7rh)

                      ==========================================

                      Would you please help resolve this error.

                      Thanks a lot for all the help.
                      Ahsan
                      • 8. Re: [JNI: Java to C Connectivity] .so file creation from g++
                        800456
                        Well, you've specified the NAME of your .so file, not the path to it: don't put the filename in the java.library.path

                        I would recommend commenting out all references to the JNI code, recompile and see if

                        java HelloWorld

                        works first. ONce you've got that, just uncomment the cladd to loadLibrary() and make sure that compiles and runs

                        then uncomment calls into the JNI and see if that works