Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
The moose likes Other JSE/JEE APIs and the fly likes call method from JAVA (jni) Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Other JSE/JEE APIs
Bookmark "call method from JAVA (jni)" Watch "call method from JAVA (jni)" New topic

call method from JAVA (jni)

bharani rao
Ranch Hand

Joined: Apr 06, 2009
Posts: 164
i have a cpp program which has the functionalities of a sip phone.
we are using pjsip stack

the program runs fine with all functionalities in sip phone and in command prompt mode.

now we have to integrate with java using jni to have a interface for the sip phone.

we have succeeded in initializing the stack( registering with the server), placing call, answering call, hanging up a call and releasing( unregistering).

while we place call, the following method in cpp gets automatically called at the reciever end.

the above method has some coding on what to do with the incoming call and some interactive things like

now we are struck up with this part for the past 1 week.
all we need to do is to have the above interactive part to be called from the java program.
for that we need to write a method for that part in java.
but the problem is that the method is automatically called when we place a call ( when some one recieves a call).so we donot need to write a native method for that in java.

it would be easier if we write a native method for the above method in java(because all the things will be called once the method is called, so do the interactive part of the method).

but we want it to be run from java.
kindly help...

thanks in advance..

Ernest Friedman-Hill
author and iconoclast

Joined: Jul 08, 2003
Posts: 24199

The C++ code can simply use the JNI to call a Java method. I'm not sure what problem you're having. This page in the JNI guide shows how to call a Java method from C++.

[Jess in Action][AskingGoodQuestions]
Rob Spoor

Joined: Oct 27, 2005
Posts: 20275

If I get the problem correctly, the native C++ code is raising events outside Java, and you want the Java code to handle those events. Am I right?

This C++ function should have a reference to the JVM (JNIEnv*) and the object on which to call the method. It can then use functions like GetMethodID and CallVoidMethod to call that method from Java. You will need to convert the parameters to the C++ function to something Java understands though.

In pseudo code:
As you see this would require ways to convert the C++ data types to Java data types and vice versa, and you're going from native code to Java code back to native code.

How To Ask Questions How To Answer Questions
bharani rao
Ranch Hand

Joined: Apr 06, 2009
Posts: 164

let me make it even more clear.
now when we run the java program in eclipse we get the gui and we can initialize the stack,place call, hang call and release the queue(unregister from server)

so when we place a call the automatically will get called which have

this code...

so we get "type a to answer the call" in the eclipse output window.
what we want is, instead of typing a in eclipse output window we want to press a button in the gui which does the function of typing a in the output eclipse window.

hope i'm more clear now.

thanks in advance
bharani rao
Ranch Hand

Joined: Apr 06, 2009
Posts: 164
sorry rob,

at the time i posted my second reply your reply was not there. that is why i posted the reply making more clear idea..

i suppose you got it right.. as far as i'm concerned what you is said is exactly what i want. but who knows, if my team lead accepts this or not....

anyways thank you very much for the exact answer....

i may reply again if that procedure is not accepted.
I agree. Here's the link:
subject: call method from JAVA (jni)
It's not a secret anymore!