can't create Java VM
843829Apr 6 2003 — edited Apr 8 2003I copied jvm.dll to my directory of the project of calling java from within c++. In my c++ code I have statements to test the creation of jvm. I got message: "jni: Can't create Java VM" I do not know what is the problem.
res = JNI_CreateJavaVM(&jvm,(void **)&env,&vm_args);
if (res < 0) {
fprintf(stderr, "jni: Can't create Java VM\n");
The code is :
#include "jni.h"
#ifdef _WIN32
#define PATH_SEPARATOR ';'
#else /* UNIX */
#define PATH_SEPARATOR ':'
#endif
#define USER_CLASSPATH "." /* where MainFrame.class is */
// user classpath
main() {
JNIEnv *env;
JavaVM *jvm;
JDK1_1InitArgs vm_args;
jint res;
jclass cls;
jmethodID mid;
jstring jstr;
jobjectArray args;
char classpath[1024];
printf("\n### welcome to the testprogram ###\n\n");
vm_args.version = 0x00010001;
JNI_GetDefaultJavaVMInitArgs(&vm_args);
/* Append USER_CLASSPATH to the end of default system class path */
sprintf(classpath, "%s%c%s",
vm_args.classpath, PATH_SEPARATOR, USER_CLASSPATH);
vm_args.classpath = classpath;
/* Create the Java VM */
res = JNI_CreateJavaVM(&jvm,(void **)&env,&vm_args);
if (res < 0) {
fprintf(stderr, "jni: Can't create Java VM\n");
// exit(1);
}
cls = env->FindClass("MainFrame");
if (cls == 0) {
fprintf(stderr, "jni: Can't find MainFrame class\n");
// exit(1);
}
mid = env->GetStaticMethodID(cls, "main", "([Ljava/lang/String;)V");
if (mid == 0) {
fprintf(stderr, "jni: Can't find MainFrame.main\n");
// exit(1);
}
jstr = env->NewStringUTF(" from C!");
if (jstr == 0) {
fprintf(stderr, "jni: Out of memory\n");
// exit(1);
}
args = env->NewObjectArray(1, env->FindClass("java/lang/String"), jstr);
if (args == 0) {
fprintf(stderr, "jni: Out of memory\n");
// exit(1);
}
env->CallStaticVoidMethod(cls, mid, args);
jvm->DestroyJavaVM();
}
------------------------