Skip to Main Content

Java HotSpot Virtual Machine

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

How to Dynamically Load jvm.dll without setting PATH

843829May 5 2010 — edited May 6 2010
Please help, invoking CreateJavaVM via a function pointer is returning -3, but I'm not sure why.

When my PATH is setup up to include the jvm.dll and I invoke JNI_CreateJavaVM() directly, it works fine. But I want it to work regardless of my PATH setting, so I am trying to use LoadLibrary and dynamically load the jvm.dll and then a function pointer to invoke CreateJavaVM().

The LoadLibrary returns non-NULL result, the GetProcAccress() returns non-null, but when I invoke the pointer to the CreateJavaVM call, it returns -3. I know -3 is JNI_EVERSION error, but any ideas what is wrong with the code below, such that it would give me this error?

Currently my code is as follows:
typedef jint (JNICALL CreateJavaVM_t)(JavaVM **pvm, void **env, void *args);
JavaVM *m_jvm;
JNIEnv *m_env;

SetDllDirectory(L"C:\\Program Files\\Java\\jdk1.6.0_20\\jre\\bin\\client");
HINSTANCE hVM = LoadLibrary(L"jvm.dll");
if ( hVM == NULL ) {
   // report error
}
CreateJavaVM_t *pfnCreateJavaVM = (CreateJavaVM_t*)GetProcAddress(hVM, "JNI_CreateJavaVM");

#ifdef JNI_VERSION_1_6

char cpChars[1024] = {0};
sprintf_s(cpChars, "-Djava.class.path=%s", classPath); // classpath is defined elsewhere

JavaVMOption options[1];
options[0].optionString = cpChars;

JavaVMInitArgs vm_args;
vm_args.version = JNI_VERSION_1_6;
vm_args.options = options;
vm_args.nOptions = 1;
vm_args.ignoreUnrecognized = JNI_TRUE;
	
pin_ptr<JNIEnv*> env = &m_env;
pin_ptr<JavaVM*> jvm = &m_jvm;
int result = pfnCreateJavaVM(jvm, (void**)env, &vm_args);

 // This is where the failure occurs. result is -3 for some reason here...why?
For some reason, pfnCreateJavaVM is returning -3, but I'm not sure why? Any ideas what is wrong with this code, such that it would give me a -3?

Thanks in advance,
Bill
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Jun 3 2010
Added on May 5 2010
2 comments
890 views