More JNI and JNI_CreateJavaJVM()

J

justdoit693

I'm trying to host the JVM in a simple windows app but when I run the app
the JNI_CreateJavaJVM() call fails with -1 every time...at first, it
couldn't find the jvm.dll but I moved a copy of that into my .exe directory
and that solved that problem...is there something else I need to do with the
DLL's?

I've seen dozens of samples of this but nothing that tells me what to do for
JNI_VERSION_1_4...any help is appreciated.

Here is my code:

int APIENTRY WinMain(HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPSTR lpCmdLine,
int nCmdShow)
{
JNIEnv *env;
JavaVM *jvm;
JavaVMInitArgs vm_args;
JavaVMOption options[1];
jint res;
jclass cls;
jmethodID mid;
jobjectArray args;

memset(&vm_args,0,sizeof(vm_args)) ;
vm_args.version = JNI_VERSION_1_4 ;
vm_args.nOptions = 1;
options[0].optionString = "-Djava.class.path=d:\\MyApp.jar";
vm_args.options = options;
vm_args.ignoreUnrecognized = true;

res = JNI_CreateJavaVM(&jvm,(void **)&env,&vm_args) ;
if (res < 0) {
MessageBox(NULL,"Failed to create JVM","Java Error",MB_OK) ;
return 0 ;
}

cls = env->FindClass("MyProg") ;
if (cls == 0) {
MessageBox(NULL,"Failed to find MyProg","Java Error",MB_OK) ;
return 0 ;
}

mid = env->GetStaticMethodID(cls, "main", "([Ljava/lang/String;)V");
if (mid == 0) {
MessageBox(NULL,"Can't find MyProg.main()","Java Error",MB_OK) ;
return 0 ;
}

args = env->NewObjectArray(0,env->FindClass("java/lang/String"), NULL);
if (args == 0) {
MessageBox(NULL,"Out of memory","MyProg",MB_OK);
return 0 ;
}

env->CallStaticVoidMethod(cls, mid, args);
jvm->DestroyJavaVM();
return 0;
}
 
J

Joseph Millar

I'm trying to host the JVM in a simple windows app but when I run the app
the JNI_CreateJavaJVM() call fails with -1 every time...at first, it
couldn't find the jvm.dll but I moved a copy of that into my .exe directory
and that solved that problem...is there something else I need to do with the
DLL's?

Bad idea. The JRE directory structure is important, that's how it
finds the locations of the core classes, among other things. If
you move jvm.dll out of its position in the JRE, you break all
relative path settings. You can do it, but it requires giving the
JVM a heck of a lot more paramters that you are here.

Simple fix is put the jre\bin\client or jre\bin\server into your
path.
I've seen dozens of samples of this but nothing that tells me what to do for
JNI_VERSION_1_4...any help is appreciated.

As of yet, I have not had the time to try my invocation programs
with JDK 1.4.2 so I don't know if there are any other gotchas.
But be aware that certain JVM verions can't find their classes
reliably and require the -Xbootclasspath option to point at the
core classes. I'll have to try it when I get some time.

--Joe
 

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,954
Messages
2,570,116
Members
46,704
Latest member
BernadineF

Latest Threads

Top