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;

vm_args.version=0x00010001;

JNI_GetDefaultJavaVMInitArgs(&vm_args);

vm_args.classpath=".:/usr/local/jdk1.2.2/include/linux/jni_md.h:/usr/local/jdk1.2.2/include$

JNI_GetDefaultJavaVMInitArgs(&vm_args);

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

if (res < 0)

{

fprintf(stderr, "Can't create Java VM\n");

exit(1);

}

(*jvm)->DestroyJavaVM(jvm);

return(0);

}

Errors:

/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

[1377 byte] By [sitay2001yo] at [2007-9-26 2:44:06]
# 1

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,

Lee

leeg2 at 2007-6-29 10:23:50 > top of Java-index,Java HotSpot Virtual Machine,Specifications...
# 2
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.
sitay2001yo at 2007-6-29 10:23:50 > top of Java-index,Java HotSpot Virtual Machine,Specifications...
# 3

Hi,

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,

Lin

linss at 2007-6-29 10:23:50 > top of Java-index,Java HotSpot Virtual Machine,Specifications...
# 4
I should also mention that the working environment is jdk1.1.8 in Slackware Linux 7.
linss at 2007-6-29 10:23:50 > top of Java-index,Java HotSpot Virtual Machine,Specifications...
# 5

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.

tayivan at 2007-6-29 10:23:50 > top of Java-index,Java HotSpot Virtual Machine,Specifications...
# 6
Location of libphi.so and libjvm.so, it also depends in which directory you have installed the jdk./usr/local/jdk1.2.2/jre/lib/i386/green_threads/libhpi.so/usr/local/jdk1.2.2/jre/lib/i386/classic/libjvm.soHope it help!
sitay2001 at 2007-6-29 10:23:50 > top of Java-index,Java HotSpot Virtual Machine,Specifications...
# 7

Hi,

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

belowUndefinedfirst referenced

symbolin file

JNI_CreateJavaVMinvoke.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..

Thanks

GaliS at 2007-6-29 10:23:50 > top of Java-index,Java HotSpot Virtual Machine,Specifications...