Reputation: 11
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
Upvotes: 0
Views: 5474
Reputation: 11
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
Upvotes: 0
Reputation: 94654
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
.
Upvotes: 1