Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.java.programmer > #25331 > unrolled thread

JNI Invocation API example

Started byKenneth Miller <kennethadammiller@gmail.com>
First post2013-09-25 10:23 -0700
Last post2013-10-01 21:48 -0700
Articles 6 — 5 participants

Back to article view | Back to comp.lang.java.programmer


Contents

  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

#25331 — JNI Invocation API example

FromKenneth Miller <kennethadammiller@gmail.com>
Date2013-09-25 10:23 -0700
SubjectJNI Invocation API example
Message-ID<ef9f31ab-60a2-4cd5-b957-a9281fedd079@googlegroups.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?

[toc] | [next] | [standalone]


#25374

FromMark <i@dontgetlotsofspamanymore.invalid>
Date2013-09-27 16:44 +0100
Message-ID<4t9b491ekq2lsdsh3g4hqu5sk5g3tff1i0@4ax.com>
In reply to#25331
On Wed, 25 Sep 2013 10:23:06 -0700 (PDT), Kenneth Miller
<kennethadammiller@gmail.com> wrote:

>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.
-- 
(\__/)  M.
(='.'=) If a man stands in a forest and no woman is around
(")_(") is he still wrong?

[toc] | [prev] | [next] | [standalone]


#25380

From"Chris Uppal" <chris.uppal@metagnostic.REMOVE-THIS.org>
Date2013-09-28 14:40 +0100
Message-ID<lfednXqjANjuQdvPnZ2dnUVZ7qWdnZ2d@bt.com>
In reply to#25331
Kenneth Miller wrote:

> What method invocation is appropriate for the println method? I'm certain
> that it isn't CallStaticVoidMethod, because that currently gives me a
> segfault.

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 

[toc] | [prev] | [next] | [standalone]


#25384

FromKenneth Miller <kennethadammiller@gmail.com>
Date2013-09-28 16:22 -0700
Message-ID<2886a957-b3a5-4d2a-8836-82cfa6d18a2e@googlegroups.com>
In reply to#25380
On Saturday, September 28, 2013 8:40:51 AM UTC-5, Chris Uppal wrote:
> Kenneth Miller wrote:
> 
> 
> 
> > What method invocation is appropriate for the println method? I'm certain
> 
> > that it isn't CallStaticVoidMethod, because that currently gives me a
> 
> > segfault.
> 
> 
> 
> 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!!

[toc] | [prev] | [next] | [standalone]


#25396

FromLew <lewbloch@gmail.com>
Date2013-09-30 11:20 -0700
Message-ID<c6fbbf2a-3418-4807-b06b-14bf0ec65f2c@googlegroups.com>
In reply to#25384
Kenneth Miller wrote:
> Chris Uppal 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.
>> 
> 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.

-- 
Lew

[toc] | [prev] | [next] | [standalone]


#25424

FromRoedy Green <see_website@mindprod.com.invalid>
Date2013-10-01 21:48 -0700
Message-ID<4c9n491togvb6e5fkfngttp4eioaorp3kr@4ax.com>
In reply to#25331
On Wed, 25 Sep 2013 10:23:06 -0700 (PDT), Kenneth Miller
<kennethadammiller@gmail.com> wrote, quoted or indirectly quoted
someone who said :

>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
-- 
Roedy Green Canadian Mind Products http://mindprod.com
Unlike many machines, computers require no water once they are
manufactured.

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.java.programmer


csiph-web