• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How to call java method from c function??

 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
How can we call the java method from c function??
Can any body have resources in this issue???
send to gali123@hotmail.com
Thanks advance
Srini
 
Ajith Kallambella
Sheriff
Posts: 5782
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am transferring this question to Java in General ( intermediate ) forum. Please use the JavaRanch forum only for issues related to the site and not for any technical discussions.
Thanks!
 
Shubhrajit Chatterjee
Ranch Hand
Posts: 356
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I am also interested in knowing the answer to this question. I have thought of ways, but none are very good regarding performance.
1. The c program invokes the jvm and runs a java program which listens for connections over socket. Then this c program communicates with the java using sockets.
2. Sim to first. Only the communication is thru shared data files.
 
Jim Bertorelli
Ranch Hand
Posts: 136
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Shubhrajit Chatterjee:

1. The c program invokes the jvm and runs a java program which listens for connections over socket. Then this c program communicates with the java using sockets.

That is what CORBA does, of course, with all the bells and whistles.
 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Gali and Shubhrajit,
I think the network option is still the best, even if not very good in terms of perf. If you go for that solution, consider using XML/RPC or SOAP.
Another solution would be to use JNI (Java Native Interface). Using the invocation API (see below), your C code can create a java virtual machine that communicates with your application.
The initialisation code should look like that:

Once created, you attach your thread to the JVM using the AttachCurrentThread method.
You can then call methods on java objects using the GetObjectClass, GetMethodID and CallXXXMethod methods on the JVM environment.
You can then detach your thread from the VM once finished.
See the tutorial on JNI on the Sun website: http://java.sun.com/docs/books/tutorial/native1.1/
The tutorial mentions that the JDK only supports attaching native threads on Win32. This was true at the time of writing but it is now possible on other systems (e.g. Solaris, Linux, etc...) as native threads are now available on those other OS.
Hope this helps,
Beno�t
[This message has been edited by Beno�t d'Oncieu (edited December 19, 2001).]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic