Error invoking JVM through JNI

B

Bruce

Hi all,

I have a problem invoking java VM by c code through JNI. The jdk is
j2sdk1.4.2_07. I use gcc in MinGW on win XP os.

I can compile and link the program correctly, but when I run the result
..exe file, it reported that the JNI_CreateJavaVM had failed.

The InvokeJVM code is as follows:
-----------------------------------------------------------------
#include <jni.h>

typedef pxdbJVM pxdbJVM;
struct pxdbJVM
{
JavaVM *jvm;
JNIEnv *jenv;
};

struct pxdbJVM pxdbJvm;

int InvokeJVM( pxdbJVM *jvm )
{
jint res = 0;

#ifdef JNI_VERSION_1_2
JavaVMInitArgs vm_args;
JavaVMOption options[2];
options[0].optionString = "-Djava.class.path=.";
options[1].optionString = "-Xmx64m";

memset( &vm_args, 0, sizeof( vm_args ));
vm_args.version = JNI_VERSION_1_2;
vm_args.options = options;
vm_args.nOptions = 2;
vm_args.ignoreUnrecognized = JNI_FALSE;

/* Create the Java VM */
res = JNI_CreateJavaVM( &(jvm->jvm), (void**)&(jvm->jenv),
&vm_args );
#else
JDK1_1InitArgs vm_args;
char classpath[1024];
vm_args.version = 0x00010001;
JNI_GetDefaultJavaVMInitArgs( &vm_args );
sprintf( classpath, "%s%c%s",
vm_args.classpath, PATH_SEPARATOR, &vm_args);
vm_args.classpath = classpath;

/* Create the Java VM */
res = JNI_CreateJavaVM( &(jvm->jvm), &(jvm->jenv), &vm_args);
#endif

return res;
}

I compile the code as follows

-----------------------------------------------------------
JDK_ROOT = C:/j2sdk1.4.2_07
JNI_HOME = -I$(JDK_ROOT)/include \
-I$(JDK_ROOT)/include/win32
DLLTOOL = dlltool
MINGW_EXTRA = d:/FreeSofts/MinGW
MINGW_INCDIR = -I$(MINGW_EXTRA)/include
MINGW_LIBDIR = $(MINGW_EXTRA)/lib


pxdbJvmTest.exe : pxdbJvmTest.c pxdbjvm.c libjvm.a
gcc -g -o $@ pxdbJvmTest.c pxdbjvm.c $(JNI_HOME) -L. -ljvm

libjvm.a: jvm.def
$(DLLTOOL) -k --input-def jvm.def --dllname jvm.dll \
--output-lib $@

jvm.def:
echo EXPORTS >jvm.def
echo JNI_CreateJavaVM@12 >> jvm.def
echo JNI_GetDefaultJavaVMInitArgs@4 >> jvm.def

clean:
rm *.exe
rm *.o
rm jvm.def
rm libjvm.a
---------------------------------------------------------
In the pxdbJvmTest.c file, I juset test the return value of InvokeJVM
function and it is reported to be -1.

To compile/link and run the problem, I have to copy the jvm.dll from
c:/j2sdk1.4.2_07/jre/bin/client/ to the working dir.

Any suggestions?

Regards
-Bruce
 
C

Chris Uppal

Bruce said:
To compile/link and run the problem, I have to copy the jvm.dll from
c:/j2sdk1.4.2_07/jre/bin/client/ to the working dir.

Never move the JVM dll away from where it lives -- it expects to find stuff
"near" it.

-- chris
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,576
Members
45,054
Latest member
LucyCarper

Latest Threads

Top