0

I am runing the follwing c++ code and the value of "ret" is -1 (the create of javavm is falid)

what is the problem with the code?

#include <iostream>
#include <conio.h>
#include <jni.h>
using namespace std;
#pragma comment (lib,"jvm.lib");

int main(){
JavaVM *jvm;
JNIEnv *env;
JavaVMInitArgs vm_args;
JavaVMOption options;

options.optionString="hello.jar";
vm_args.version=JNI_VERSION_1_6;
vm_args.nOptions=1;
vm_args.Option=&options;
vm_args.ignoreUnrecognized=0;
int ret=JNI_CreatJavaVM(&jvm,(void**)&env,&vm_args);

 }

thats for the help

Roman C
  • 49,761
  • 33
  • 66
  • 176
user3130432
  • 11
  • 2
  • 3
  • That's not a valid option for initializing a VM - You want something like `-Djava.class.path=hello.jar` instead; and initializing a VM **doesn't** actually start a main class - you'll have to do that yourself. – Anya Shenanigans Dec 23 '13 at 20:11
  • It is still returns re -1 – user3130432 Dec 24 '13 at 06:31
  • Barring the option being bad, there are several reasons why it could not start the VM - perhaps it couldn't load `jvm.dll`. Have you tried running the code while observing the app with filemon/regmon? – Anya Shenanigans Dec 24 '13 at 11:19
  • No how can i check if the jvm.dll was laoded? And i am working with the debuger at visual studio – user3130432 Dec 24 '13 at 12:04

2 Answers2

1

If you pass in an invalid option to the CreateJavaVM call, because of the ignoreUnrecognized = 1 it will fail to launch the JVM.

When I create a simple jar with a public static void main method, and use the following code, it correctly invokes the main method:

JavaVM *jvm;
JNIEnv *env;
JavaVMInitArgs vm_args;
JavaVMOption options;
options.optionString = "-Djava.class.path=hello.jar";
vm_args.version = JNI_VERSION_1_6;
vm_args.nOptions = 1;
vm_args.options = &options;
vm_args.ignoreUnrecognized = 0;
int ret = JNI_CreateJavaVM(&jvm, (void**)&env, &vm_args);
if (ret == 0) {
    jclass cls = env->FindClass("hello");
    if (cls != 0) {
        jmethodID meth = env->GetStaticMethodID(cls, "main", "([Ljava/lang/String;)V");
        jarray args = env->NewObjectArray(0, env->FindClass("java/lang/String"), 0);
        env->CallStaticVoidMethod(cls, meth, args);
    }
}
return ret;

This assumes that hello.jar is in the working directory of the executable. If it isn't then you need to specify the full path to the jar file. e.g. c:\hello.jar.

Anya Shenanigans
  • 91,618
  • 3
  • 107
  • 122
  • What about the jvm.dll load? – user3130432 Dec 25 '13 at 08:10
  • If the `jvm.dll` does not load then the app won't even start (I was thinking of the situation where the dll is delay loaded, which is not the default). Your initial code simply won't work as you're using an invalid option. – Anya Shenanigans Dec 25 '13 at 08:37
0

I wrote the next code now:

#include "stdafx.h"
#include <iostream>
#include "jni.h"
#include <conio.h>
using namespace std;

#pragma comment (lib,"jvm.lib")

int main()

    {  
JavaVM *jvm;
JNIEnv *env;
JavaVMInitArgs vm_args;
JavaVMOption options;

options.optionString="-Djava.class.path=hello.jar";
vm_args.version=JNI_VERSION_1_6;
vm_args.options=&options;
vm_args.ignoreUnrecognized=0;
int ret=JNI_CreateJavaVM(&jvm,(void**)&env,&vm_args);


     }

and it's writen me the follwing erore when i try to run :

Error 1 error LNK2019: unresolved external symbol _imp_JNI_CreateJavaVM@12 referenced in function _main C:\Users\Hilla\Documents\Visual Studio 2012\helloworld\helloworld\helloworld.obj helloworld

jvm.lim and jvm.dll are at the working directory

thanks for the help

user3130432
  • 11
  • 2
  • 3