Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.java.programmer > #25331
| Newsgroups | comp.lang.java.programmer |
|---|---|
| Date | 2013-09-25 10:23 -0700 |
| Message-ID | <ef9f31ab-60a2-4cd5-b957-a9281fedd079@googlegroups.com> (permalink) |
| Subject | JNI Invocation API example |
| From | Kenneth Miller <kennethadammiller@gmail.com> |
I'm trying to write a simple C++ example to call into the JNI so that I can call 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?
Back to comp.lang.java.programmer | Previous | Next — Next in thread | Find similar | Unroll thread
JNI Invocation API example Kenneth Miller <kennethadammiller@gmail.com> - 2013-09-25 10:23 -0700
Re: JNI Invocation API example Mark <i@dontgetlotsofspamanymore.invalid> - 2013-09-27 16:44 +0100
Re: JNI Invocation API example "Chris Uppal" <chris.uppal@metagnostic.REMOVE-THIS.org> - 2013-09-28 14:40 +0100
Re: JNI Invocation API example Kenneth Miller <kennethadammiller@gmail.com> - 2013-09-28 16:22 -0700
Re: JNI Invocation API example Lew <lewbloch@gmail.com> - 2013-09-30 11:20 -0700
Re: JNI Invocation API example Roedy Green <see_website@mindprod.com.invalid> - 2013-10-01 21:48 -0700
csiph-web