• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

JNI_CreateJavaVM returns with -1

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi there,
I cannot start the VM with a C++ method. The method looks
like this:
void main (int argc, char *argv [])
{
JNIEnv *env;
JavaVM *jvm;
JavaVMInitArgs vm_args;
JavaVMOption options[2];
jint res;
jclass cls;
jmethodID mid;
jstring jstr;
jobjectArray args;
char classpath[1024];
char buffer [512], *psz;
// JNI_GetDefaultJavaVMInitArgs(&vm_args);
options[0].optionString = ".";
// options[0].optionString = "-verbose:jni";
options[1].optionString = "-Djava.compiler=NONE";
vm_args.version = JNI_VERSION_1_2;
vm_args.options = options;
vm_args.nOptions = 2;
vm_args.ignoreUnrecognized = JNI_FALSE;
/* Create the Java VM */
res = JNI_CreateJavaVM(&jvm, (void**)&env, &vm_args);
fprintf(stderr, "Starting VM\n");
if (res < 0)
{
switch ( res )
{
case -1:
fprintf(stderr, "/* unknown error */\n");
break;
case -2:
fprintf(stderr, "/* thread detached from the VM */\n");
break;
case -3:
fprintf(stderr, "/* JNI version error */\n");
break;
case -4:
fprintf(stderr, "/* not enough memory */\n");
break;
case -5:
fprintf(stderr, "/* VM already created */\n");
break;
case -6:
fprintf(stderr, "/* invalid arguments */\n");
break;
default:
fprintf(stderr, "Can't create Java VM\n");
}
exit(1);
return 123;
}

fprintf(stderr, "Seems to run");
cls = env->FindClass("te");
if (cls == 0) {
fprintf(stderr, "Can't find hello class\n");
exit(1);
}
jvm->DestroyJavaVM();
// return 0;

}

The program returns with -1 (unknow error). How can I get
some more detailed information on what is the problem ?
Did anyone make the same experience ?
Regards,
Tobias
 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Isn't something missing from your options[0] setting?
options[0].optionString = "-Djava.class.path=.:<where rt.jar>";
LX

Originally posted by Tobias Pfeifer:
Hi there,
I cannot start the VM with a C++ method. The method looks
like this:
void main (int argc, char *argv [])
{
JNIEnv *env;
JavaVM *jvm;
JavaVMInitArgs vm_args;
JavaVMOption options[2];
jint res;
jclass cls;
jmethodID mid;
jstring jstr;
jobjectArray args;
char classpath[1024];
char buffer [512], *psz;
// JNI_GetDefaultJavaVMInitArgs(&vm_args);
options[0].optionString = ".";
// options[0].optionString = "-verbose:jni";
options[1].optionString = "-Djava.compiler=NONE";
vm_args.version = JNI_VERSION_1_2;
vm_args.options = options;
vm_args.nOptions = 2;
vm_args.ignoreUnrecognized = JNI_FALSE;
/* Create the Java VM */
res = JNI_CreateJavaVM(&jvm, (void**)&env, &vm_args);
fprintf(stderr, "Starting VM\n");
if (res < 0)
{
switch ( res )
{
case -1:
fprintf(stderr, "/* unknown error */\n");
break;
case -2:
fprintf(stderr, "/* thread detached from the VM */\n");
break;
case -3:
fprintf(stderr, "/* JNI version error */\n");
break;
case -4:
fprintf(stderr, "/* not enough memory */\n");
break;
case -5:
fprintf(stderr, "/* VM already created */\n");
break;
case -6:
fprintf(stderr, "/* invalid arguments */\n");
break;
default:
fprintf(stderr, "Can't create Java VM\n");
}
exit(1);
return 123;
}

fprintf(stderr, "Seems to run");
cls = env->FindClass("te");
if (cls == 0) {
fprintf(stderr, "Can't find hello class\n");
exit(1);
}
jvm->DestroyJavaVM();
// return 0;

}

The program returns with -1 (unknow error). How can I get
some more detailed information on what is the problem ?
Did anyone make the same experience ?
Regards,
Tobias


 
Tobias Pfeifer
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by lichade xun:
Isn't something missing from your options[0] setting?
options[0].optionString = "-Djava.class.path=.:<where rt.jar>";
LX


Hi,
that doesn't work either. Do you need to add this in the option part ? I thought that the "JavaHome" value in the registry was used to find the runtime classes.
Regards,
Tobias
 
Ranch Hand
Posts: 308
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i know, the answer is most likely a bit late (first post was 2001 , the reason why i am answering is, that there has not been a possible solution to the symptom JNI_CreateJavaVM() returning -1 which solved my problem.

in my case jvm.dll must be kept in its installation directory. so don't just copy jvm.dll into the same directory of your JNI-application!
it seems that jvm.dll is somehow using paths relative to its directory of java installation dir.

that means, that you need to set $PATH (or another appropriate env-var) to the directory where jvm.dll sits (JAVA_INSTALL_DIR/jre/bin/client/) so the system finds the jvm.dll when it is needed from your app.
 
reply
    Bookmark Topic Watch Topic
  • New Topic