JNI_CreateJavaVM() prevents loading the DLL

C

Chao Dong

I am trying to create a JVM from a DLL, however, when including the
call to JNI_CreateJavaVM(), the DLL will fail to get loaded (with
error code of 126 which means failed to locate the module). If comment
this function call out, then the DLL load was fine.

I've got this problem once but left it behind until it mysteriously
disappeared after I have fixed some irrelevant bugs and come back to
the problem. Now it happens again when I try to run this DLL on a
laptop. My Pc is running Windows 2000 5.00.2195 Service Pack 2 with
JRE 1.4.1_01, while the laptop is running Windows 2000 5.00.2195
Service Pack 3 with JRE 1.4.1_02. VC6 is used to compile and link the
DLL.

Any comments will be highly appreciated.

The following is the standard code to create JVM:

JavaVMInitArgs vm_args;
jint retValue=1;

vm_args.version = JNI_VERSION_1_2;
vm_args.ignoreUnrecognized = JNI_TRUE;
vm_args.nOptions = 1;
JavaVMOption options[1];
char classPath[256];

strcpy(classPath, "-Djava.class.path=.\");
options[0].optionString = classPath;
options[0].extraInfo = NULL;
vm_args.options = options;

//The inclusion of the following call will fail the load of this DLL
//while it's fine if comment it out (jvm and env are declared
somewhere else)
retValue = JNI_CreateJavaVM(&jvm, (void **) &env, &vm_args);


Regards
Chao
 
J

Joseph Millar

On 15 Jul 2003 11:45:15 -0700, (e-mail address removed) (Chao Dong) wrote:
[snip!]
//The inclusion of the following call will fail the load of this DLL
//while it's fine if comment it out (jvm and env are declared
somewhere else)
retValue = JNI_CreateJavaVM(&jvm, (void **) &env, &vm_args);

The JNI Invocation API's are exported from jvm.dll
which is locted in the JRE (exact location varies
by platform). I assume you are not dynloading
jvm.dll and using a pointer to the function, which
means your program is failing to load at runtime.
The most likely problem is your PATH does not have
the jre\bin\client or jre\bin\server directory
present anywhere.

I suspect this:

1. You are linking your code against jdk\lib\jvm.lib
2. Your PATH does not include jre\bin\client or
jre\bin\server in your PATH.
3. You are running your program and it's immediately
failing because jvm.dll is not to found anywhere.

Couple possible solutions:

1. Include jre\bin\client or jre\bin\server in your
PATH env variable. This is simple and requires
no code changes to work.
2. Dynload jvm.dll (LoadModule() and it's friends)
and then use GetProcAddress() to find the function
and call it through that pointer. This is more
flexible than the above code (and is how the
java.exe tool does it), but requires much more
work and you have to be able to find jvm.dll.
There are any number of ways to find jvm.dll
(Registry, configuration option, etc).

Up to you and your needs.

Good luck.

--Joe
 
Joined
Apr 16, 2008
Messages
1
Reaction score
0
JNI_CreateJavaVM(&jvm, (void**)&env, &vm_args) Fails to Load JVM

#include "D:\Program Files\Java\jdk1.6.0_05\include\jni.h"
#include <iostream>
#include <windows.h>

#define PATH_SEPARATOR ';' /* define it to be ':' on Solaris */
#define USER_CLASSPATH "." /* where Prog.class is */

main() {
JNIEnv *env;
JavaVM *jvm;
jint res;
jclass cls;
jmethodID mid;
jstring jstr;
jclass stringClass;
jobjectArray args;

JavaVMInitArgs vm_args;

JavaVMOption options[1];

options[0].optionString =
"-Djava.class.path=" USER_CLASSPATH;

vm_args.version = 0x00010006;
vm_args.options = options;
vm_args.nOptions = 1;
vm_args.ignoreUnrecognized = JNI_TRUE;

/*
std::string java_home;
java_home = getenv("JAVA_HOME");
java_home.append("\\jre\\bin\\client\\jvm.dll");
std::cout << "\n\n*****Loading jvm.dll library******\n\n";
HINSTANCE hJVM = LoadLibrary(java_home.c_str());
if (hJVM == NULL) {
std::cout << "\n\n*****Failed Loading jvm.dll library******\n\n";
}
*/


/* Create the Java VM */
res = JNI_CreateJavaVM(&jvm, (void**)&env, &vm_args);

if (res < 0) {
fprintf(stderr, "Can't create Java VM\n");
//exit(1);
}
cls = env->FindClass("Prog");
if (cls == NULL) {
goto destroy;
}

mid = env->GetStaticMethodID( cls, "main",
"([Ljava/lang/String;)V");
if (mid == NULL) {
goto destroy;
}
jstr = env->NewStringUTF( " from C!");
if (jstr == NULL) {
goto destroy;
}
stringClass = env->FindClass( "java/lang/String");
args = env->NewObjectArray( 1, stringClass, jstr);
if (args == NULL) {
goto destroy;
}
env->CallStaticVoidMethod( cls, mid, args);

destroy:
if (env->ExceptionOccurred()) {
env->ExceptionDescribe();
}
jvm->DestroyJavaVM();

//FreeLibrary(hJVM);
return 0;
}
 

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

Staff online

Members online

Forum statistics

Threads
473,764
Messages
2,569,566
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top