Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > Calling back to C when started with invocation API

Reply
Thread Tools

Calling back to C when started with invocation API

 
 
naleiden
Guest
Posts: n/a
 
      06-20-2006
Is it possible to call native methods in a Java program which has
been started with the invocation API, where the navive methods are
implemented in the same C program that started the Java, i.e.:

/////////////////
// C Code //
/////////////////

// start JVM from C
void do_java_work () {
JNI_CreateJavaVM(&jvm, &env, &vm_args);
jclass cls = env->FindClass("Main");
jmethodID mid = env->GetStaticMethodID(cls, "test", "()Z");
env->CallStaticVoidMethod(cls, mid, 0);
...

}

JNIEXPORT jboolean JNICALL Java_doSomething (JNIEnv *env, jobject obj)
{
...

}

// End C code..

/////////////////////
// Java Code //
/////////////////////

public class Main {

public static void test (String args[]) {
doSomething();
}

public native boolean doSomething ();

}

// End Java Code

I know this will not work as currently coded, but can anyone tell me
how to do it, or how to do something equivalent?

Thanks,

-Nick

 
Reply With Quote
 
 
 
 
Gordon Beaton
Guest
Posts: n/a
 
      06-20-2006
On 20 Jun 2006 09:55:46 -0700, naleiden wrote:
> Is it possible to call native methods in a Java program which has
> been started with the invocation API, where the navive methods are
> implemented in the same C program that started the Java, i.e.:


Yes. The only difference from doing it the "regular" way is that you
don't need to load the library containing the native methods in order
to call them.

You still need to make sure your methods follow the JNI naming and
calling convention (which kind of implies that you need to use javah),
or you can use RegisterNatives() to associate your native methods with
the class that will use them.

> I know this will not work as currently coded, but can anyone tell me
> how to do it, or how to do something equivalent?


Except that it isn't complete, I can't see anything blatantly wrong
with the example you posted.

/gordon

--
[ do not email me copies of your followups ]
g o r d o n + n e w s @ b a l d e r 1 3 . s e
 
Reply With Quote
 
 
 
 
Chris Uppal
Guest
Posts: n/a
 
      06-20-2006
naleiden wrote:

> Is it possible to call native methods in a Java program which has
> been started with the invocation API, where the navive methods are
> implemented in the same C program that started the Java,


Yes.

You will probably want to use the JNI function RegisterNatives() to tell the
JVM what implementation it should use for whatever native methods you define in
your outer C program. (That's instead of the typical JNI pattern where the
java class loads a library containing that code in its static initialiser).

If you do that then names of the functions you use are irrelevant to the JVM
(since it's working directly with function pointers instead of looking up
functions by name in a DLL/.so file); but the parameters, calling convention,
and return type should be /exactly/ as they are generated by javah.

So in your example, you would use RegisterNatives() to tell the JVM that the
implementation of Main.doSomething() was provided by the function pointed to by
&Java_doSomething.

-- chris



 
Reply With Quote
 
naleiden
Guest
Posts: n/a
 
      06-20-2006
Thanks for your quick reply Gordon! I feel compelled to tell you that
recognizing your name (from many of your posts that I have used
previously) gives me confidence in venturing forward!

 
Reply With Quote
 
naleiden
Guest
Posts: n/a
 
      06-20-2006
Thanks for the further clarification Chris!

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Calling OSX API similar to calling Win32 API? Thomas Thomassen Ruby 2 12-29-2010 12:37 AM
Calling requiredif on "save" invocation Gearóid O'Ceallaigh Java 1 08-11-2008 08:28 AM
if a variable is started inside a closure, can it ever be redeclared and started over, outside of the original function? Jake Barnes Javascript 2 02-15-2006 03:32 AM
Calling the C API from Python and Python program from same C API -bidirectional Praveen, Tayal (IE10) Python 0 03-17-2005 06:33 AM
JNI- Invocation API problem lucas Java 1 11-15-2004 11:57 AM



Advertisments