JNI Invocation API example

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?
 
M

Mark

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?

I suggest you read a JNI tutorial (or two) before continuing. There
are several mistakes in your example and I think a tutorial would
clear things up for you.
 
K

Kenneth Miller

Kenneth Miller wrote:








It looks to me as if you might benefit from learning a little more Java first.



Try writing a "Hello World" program in Java, then see the difference between

what the Java program is telling the JVM to do, and what your JNI code is

(attempting) to tell the JVM to do. In particular println() is not a static

method, and as such you need an instance of PrintStream.



By the way: you might want to have a look at "jace",

http://code.google.com/p/jace/wiki/Overview (caveat: I haven't used it myself).



-- chris

Yeah, I ended up getting it to work later that night. I've known Java pretty well, I just didn't know what all functions were available from the env pointer. And I wasn't asking to be referred to the documentation. I was specifically asking for an example. Anyway, now that I have a working version, I'll read the whole documentation and then move on to possibly Jace.

Thanks a lot for that Jace tool!!
 
L

Lew

Kenneth said:
Yeah, I ended up getting it to work later that night. I've known Java pretty well, I just didn't know what all
functions were available from the env pointer. And I wasn't asking to be referred to the documentation. I
was specifically asking for an example. Anyway, now that I have a working version, I'll read the whole
documentation and then move on to possibly Jace.

You came here for help. You don't really get to predetermine the form of that help.

If you had all the answers you wouldn't need to ask for assistance. Since you already
know you need more information, and Chris's answer was, after all, based entirely on
what you said, I recommend you open your mind to whatever form the advice takes.

Oh, and reading documentation is sometimes seen as a first, or at least early step.
 
R

Roedy Green

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 pertin=
ent 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 giv=
e me a working example sufficient to call System.out.println("hello world")=
.=20

download the source code for Setclock at
http://mindprod.com/products.html
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,813
Messages
2,569,696
Members
45,483
Latest member
TedDvb6626

Latest Threads

Top