K
Kenneth Miller
I'm trying to write a simple C++ example to call into the JNI so that I cancall Java from C/++. I know this may sound off topic, but it's very pertinent to Java. I'm not that familiar with the JNI API, and I'm still reading through the docs, but I was hoping that someone would be kind enough to give me a working example sufficient to call System.out.println("hello world")..
Here's my current source (I'm pretty sure that if you just throw this into a int main(void) it will work):
JavaVM *jvm; /* denotes a Java VM */
JNIEnv *env; /* pointer to native method interface */
JavaVMInitArgs vm_args; /* JDK/JRE 6 VM initialization arguments */
JavaVMOption* options = new JavaVMOption[1];
options[0].optionString = const_cast<char*>( "-Djava.class.path=/usr/lib/java");
vm_args.version = JNI_VERSION_1_6;
vm_args.nOptions = 1;
vm_args.options = options;
vm_args.ignoreUnrecognized = false;
/* load and initialize a Java VM, return a JNI interface
* pointer in env */
if (JNI_CreateJavaVM(&jvm,(void **) &env, &vm_args)!=JNI_OK)
cout << "JNI not JNI_OK" << endl;
if (jvm==NULL)
cout << "jvm is null!" << endl;
if (env==NULL)
cout << "env is null!" << endl;
delete options;
cout.flush();
/* invoke the Main.test method using the JNI */
jclass cls = env->FindClass("java/io/PrintStream");
if (cls==NULL)
cout << "class is null" << endl;
cout.flush();
jmethodID mid = env->GetMethodID(cls, "println", "(I)V");
if (mid==NULL)
cout << "method is null" << endl;
else
env->CallStaticVoidMethod(cls, mid, "hello world");
/* We are done. */
//jvm->DestroyJavaVM();
Here are the questions that I have:
What method invocation is appropriate for the println method? I'm certain that it isn't CallStaticVoidMethod, because that currently gives me a segfault.
How do I determine what that "(I)V" argument is to GetMethodID? Is that some kind of signature? What does "(I)" and "V" Stand for?
Here's my current source (I'm pretty sure that if you just throw this into a int main(void) it will work):
JavaVM *jvm; /* denotes a Java VM */
JNIEnv *env; /* pointer to native method interface */
JavaVMInitArgs vm_args; /* JDK/JRE 6 VM initialization arguments */
JavaVMOption* options = new JavaVMOption[1];
options[0].optionString = const_cast<char*>( "-Djava.class.path=/usr/lib/java");
vm_args.version = JNI_VERSION_1_6;
vm_args.nOptions = 1;
vm_args.options = options;
vm_args.ignoreUnrecognized = false;
/* load and initialize a Java VM, return a JNI interface
* pointer in env */
if (JNI_CreateJavaVM(&jvm,(void **) &env, &vm_args)!=JNI_OK)
cout << "JNI not JNI_OK" << endl;
if (jvm==NULL)
cout << "jvm is null!" << endl;
if (env==NULL)
cout << "env is null!" << endl;
delete options;
cout.flush();
/* invoke the Main.test method using the JNI */
jclass cls = env->FindClass("java/io/PrintStream");
if (cls==NULL)
cout << "class is null" << endl;
cout.flush();
jmethodID mid = env->GetMethodID(cls, "println", "(I)V");
if (mid==NULL)
cout << "method is null" << endl;
else
env->CallStaticVoidMethod(cls, mid, "hello world");
/* We are done. */
//jvm->DestroyJavaVM();
Here are the questions that I have:
What method invocation is appropriate for the println method? I'm certain that it isn't CallStaticVoidMethod, because that currently gives me a segfault.
How do I determine what that "(I)V" argument is to GetMethodID? Is that some kind of signature? What does "(I)" and "V" Stand for?