7 Replies Latest reply: Dec 20, 2001 6:08 AM by 843829 RSS

    Error in creating JVM, need help!

      I am just trying a simple c program to create a JVM on a linux machine. However it always result errors during compilation. I got no idea what's went wrong.

      I am using jdk1.2.

      #include <jni.h>
      #include <stdlib.h>

      int main( int argc, char *argv[] )
      JNIEnv *env;
      JavaVM *jvm;
      JDK1_1InitArgs vm_args;
      jint res;

      res=JNI_CreateJavaVM(&jvm,(void **)&env,&vm_args);

      if (res < 0)
      fprintf(stderr, "Can't create Java VM\n");

      /tmp/ccvMj208.o: In function `main':
      /home/bettylo/2/invoke.c:12: undefined reference to `JNI_GetDefaultJavaVMInitArgs'
      /home/bettylo/2/invoke.c:14: undefined reference to `JNI_GetDefaultJavaVMInitArgs'
      /home/bettylo/2/invoke.c:15: undefined reference to `JNI_CreateJavaVM'
      collect2: ld returned 1 exit status


      This is the command I use to compile the c program

      gcc -I$JAVA_HOME/include/linux -I$JAVA_HOME/include -o invoke.exe invoke.c

        • 1. Re: Error in creating JVM, need help!
          You need to link against the JDK library that provides those functions, which is the jvm library

          This is a good starting point and it mentions what you need to do to link and compile : http://web2.java.sun.com/docs/books/tutorial/native1.1/index.html
          specifically this http://web2.java.sun.com/docs/books/tutorial/native1.1/invoking/invo.html shows the commands needed

          Hope this helps,
          • 2. Re: Error in creating JVM, need help!
            Yes, thank you.

            I also need to add the path of libhpi.so and libjvm.so to the LD_LIBRARY_PATH. I have omitted that before.
            • 3. Re: Error in creating JVM, need help!

              I get the simlilar error when trying to use the JNI_CreateJavaVM(..), JNI_GetDefaultJavaVMInitArgs(..), how can I set the LD_LIBRARY_PATH on Linux? I just find the "libjava.so" in the lib subdirectory, where do the "libhpi.so" & "libjvm.so" files reside? Some instructions mention that we need to use the "ldconfig" command to link the soname to the real name before setting the LD_LIBRARY_PATH, how about no "ldconfig" command available in linux?

              Thanks for any clue,
              • 4. Re: Error in creating JVM, need help!
                I should also mention that the working environment is jdk1.1.8 in Slackware Linux 7.
                • 5. Re: Error in creating JVM, need help!
                  You can set the LD_LIBRARY_PATH in linux with the follow command :
                  Replace whatever specific library path you need in the below example!

                  Red Hat Linux specific format :
                  export LD_LIBRARY_PATH=<your lib path>:<other paths required>:$LD_LIBRARY_PATH

                  Another small example :
                  export LD_LIBRARY_PATH=/usr/java/jsdk1.2/jre/lib:$LD_LIBRARY_PATH

                  Only after loading the library environment will you be able to run the JVM successfully.

                  Your other question on the segmentation core dump is probably due to the fact that you use a (void**) pointer with the old 1.1 vm_args.version. JDK versions lower than 1.2 uses the older interface that is not as portable and takes a JNIEnv pointer in the

                  JNI_CreateJavaVM(JavaVM* ,JNIEnv **,JDK1_1InitArgs);

                  method but newer 1.2 uses

                  JNI_CreateJavaVM(JavaVM* ,void **,JavaVMInitArgs);

                  Read the updated docs on 1.2 from your JDK distribution and you will realize that the segmentation crush was due to pointer problems. Note also that the code to invoke the 1.2 version and 1.1 versions are different, so read the docs carefully.

                  • 6. Re: Error in creating JVM, need help!
                    Location of libphi.so and libjvm.so, it also depends in which directory you have installed the jdk.


                    Hope it help!
                    • 7. Re: Error in creating JVM, need help!

                      I am using the example ie in chap7 of http://java.sun.com/docs/books/tutorial/native1.1/invoking/invo.html ie invoke.c ..

                      In invoke.c program we are loading jvm for calling java methods in this c program..

                      while compiling the using the you specified command ie

                      cc -I/usr/java2/include -I/usr/java2/include/unixware -L/usr/java2/jre/lib/x86at -o invoke invoke.c

                      i am getting the errors like

                      belowUndefined first referenced
                      symbol in file
                      JNI_CreateJavaVM invoke.o
                      UX:ld: ERROR: invoke: fatal error: Symbol referencing errors. No output written
                      to invoke

                      tell me how to run and execute the this c function

                      pls send to gali123@hotmail.com

                      it's very Urgent to us..