JNI_CreateJavaVM fails (returns -1)

B

buzz

Hello folks,
I am trying to get a simple windows console application to lauch the jvm
but the call to JNI_CreateJavaVM keeps failing (returns -1). What should be
fairly straightforward (based on the examples/api etc) has consumed a lot of
my time and so I am turning to the community at large that can tell me what
is going on here.
I have tried just about everything (with the paths, client dll, server dll,
paths etc.) but nothing seems to work. I have checked the groups did not
find anything that works for me.

I even removed the windows VM just to make sure that it was not coming into
the
picture since it had it's own java related dll's in the system dirs.

Here is the code (it fails on the call to JNI_CreateJavaVM):
(the call to launching the java class is commented out since i was not get
past the
problem of creating the jvm)

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

JNIEnv* create_vm() {
JavaVM* jvm;
int res=0;
JNIEnv* env = NULL;
JavaVMInitArgs vm_args;
JavaVMOption options[1];

//memset(&vm_args, 0, sizeof(vm_args));
//vm_args.version = JNI_VERSION_1_2;/*this does not work either*/
vm_args.version = JNI_VERSION_1_4;
vm_args.nOptions = 1;
options[0].optionString = "-Djava.class.path=.";
vm_args.options = options;

vm_args.ignoreUnrecognized = JNI_FALSE;
res = JNI_CreateJavaVM(&jvm, (void **)&env, &vm_args);
if (res < 0) {
printf("error creating jvm, return code=%d\n",res);
return NULL;
}

return env;
}

/*void invoke_class(JNIEnv* env) {
jclass helloWorldClass;
jmethodID mainMethod;
jobjectArray applicationArgs;
jstring applicationArg0;

helloWorldClass = (*env)->FindClass(env, "InvocationHelloWorld");

mainMethod = (*env)->GetStaticMethodID(env, helloWorldClass, "main",
"([Ljava/lang/String;)V");

applicationArgs = (*env)->NewObjectArray(env, 1, (*env)->FindClass(env,
"java/lang/String"), NULL);
applicationArg0 = (*env)->NewStringUTF(env, "From-C-program");
(*env)->SetObjectArrayElement(env, applicationArgs, 0, applicationArg0);

(*env)->CallStaticVoidMethod(env, helloWorldClass, mainMethod,
applicationArgs);
}*/


int main(int argc, char **argv) {

JNIEnv* env = create_vm();
if(env == NULL)
{
fprintf(stderr, "Can't create Java VM\n");
exit(-1);
}
//invoke_class( env );
}

I use the jvm.lib that is j2sdk's \lib directory
========
Directory files:
D:\jnitest3\Debug>ls
InvocationHelloWorld.class test3.ilk
jvm.dll test3.pch
jvmfromc.obj test3.pdb
setpath.bat vc60.idb
spath3.bat vc60.pdb
test3.exe
====
output:
D:\jnitest3\Debug>test3.exe
error creating jvm, return code=-1
Can't create Java VM

D:\jnitest3\Debug>

========
my environment:
window xp pro on a dell laptop
D:\jnitest3>java -version
java version "1.4.2_03"
Java(TM) 2 Runtime Environment, Standard Edition (build 1.4.2_03-b02)
Java HotSpot(TM) Client VM (build 1.4.2_03-b02, mixed mode)

C Comiler:Visual C++ 6.0
(I also have Microsoft Visual Studio .Net on the machine..i hope it's not
interfering
with the visual c++ 6.0)

Here is partof the path settings:
Path=.;d:\j2sdk1.4.2_03\bin;D:\jwsdp-1.3\bin;d:\jwsdp-1.3\apache-ant\bin;C:\
mks\
mksnt;C:\WINDOWS\system32;C:\WINDOWS;C:\WINDOWS\system32\WBEM;C:\PROGRA~1\CO
MMON
===============
thanks,
buzz.
 

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

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,022
Latest member
MaybelleMa

Latest Threads

Top