Results 1 to 14 of 14
Thread: Java and C++ communication
- 12-21-2010, 12:00 PM #1
Member
- Join Date
- Dec 2010
- Posts
- 9
- Rep Power
- 0
Java and C++ communication
Hi guys
i have a C++ code and java code. C++ code writes records to java program and java should process those records and send the resultant records to c++ code. I have implemented a native methods in c++ to getrecord and outputresult. And one more thing is that java program is called from c++ program using System command
Now what i am facing is when ever java tries to read a record from c++ code a new instance of c++ code is creating and the values that are already written in c++ code are not coming.
finally my question is when a C++ function is called from java how to return global variables in C++ program.
- 12-21-2010, 12:02 PM #2
Member
- Join Date
- Dec 2010
- Posts
- 9
- Rep Power
- 0
And one more thing is my C++ program is already in running state and those global variables are modified before the function is called in java program.
- 12-22-2010, 06:28 AM #3
Member
- Join Date
- Dec 2010
- Posts
- 9
- Rep Power
- 0
one more thing is that my C++ programs calls java program using system command and after that both programs should exchange data. I already mentioned this. but i am emphasizing on this again because in jni implementation i am getting problem(leave it). is there a way to do it socket programming too
- 12-22-2010, 04:12 PM #4
Senior Member
- Join Date
- Jan 2009
- Posts
- 671
- Rep Power
- 5
When you execute a system call in c++, you are creating a new process and you don't even have direct access to it's pipes. If you want interprocess communication, you will have to use files, or sockets, shared memory, named pipes, etc. There is no standardized way for one process to directly access the internals of another (it's possible to do it via hacks, but god help you if you go down that road).
Sockets will work fine, but keep in mind that sockets are not enabled by default on all systems. Some fire walls are very restrictive and will prevent socket based interprocess communication. Also, on Windows, if you don't have a network connection and have not set up a loopback network, sockets are disabled.
I'd probably either go with JNI, or I'd change the system call to a fork, and use the stdin/stdout of the new subprocess for the communication link - or if the amount of communication you need is very limited to just startup info, you can pass that info in as part of the command line, or use a file drop box.
- 12-23-2010, 03:57 AM #5
Member
- Join Date
- Dec 2010
- Posts
- 9
- Rep Power
- 0
Thanq
Actually we have namedpipe implementation for communication. And now i want to implement JNI for this. Communication between both the programs is high.fork() system call is used to at the time of executing java program. Which one will be a better choice namedpipe or JNI?
if JNI is better then How to make both of them communicate so that records can be passed from C++ process to java Process?
Thank you in advance
- 12-23-2010, 03:46 PM #6
Senior Member
- Join Date
- Jan 2009
- Posts
- 671
- Rep Power
- 5
As to whether or JNI makes more sense than pipes, it really just depends. JNI makes your builds more cumbersome, but once you've gotten past that, deliveries are pretty standard. With named pipes, you're going to have to have a bunch of #ifdef statements to handle the various operating systems you might deploy on.
JNI gives you a much tighter coupling between the native code and the java side, which can be good or bad depending on your application. For example, suppose you wrote an mpeg compression package in native code and wanted to use it from java. You could write a JNI wrapper around it and use it that way, but you could also write a standalone application and use it 'out of the box' with pipes. You could use the exact same 'out of the box' implementation in a php application.
You really need to do a trade study to help you decide for your particular application. I've used both approaches myself. Neither is universally the best option.
...one other consideration. None of the IDEs I've used do a very good job of dealing with mixed language applications. Using pipes will allow you to use a java-oriented IDE (or project) for the java side and a c++ oriented IDE (or project) for the native side ....may or may not be a concern for you.Last edited by toadaly; 12-23-2010 at 03:48 PM.
- 01-03-2011, 04:05 AM #7
Member
- Join Date
- Dec 2010
- Posts
- 9
- Rep Power
- 0
Now i got a problem
As i mentioned earlier my C++ and java programs should communicate. And java code is called as a child process to c++ using fork command. when ever i am using JNI (I implemented a native methods in c++ ) a new instance of c++ is creating and i am not able get the correct record instead of that i am getting null. What should i do to make both child and parent process(java n c++) to communicate using jni?
- 01-03-2011, 05:26 AM #8
Senior Member
- Join Date
- Jan 2009
- Posts
- 671
- Rep Power
- 5
You can't directly communicate between a parent and a child process using JNI. If you want to communicate between a parent and child process, the simplest way is through pipes, for example, stdin and stdout.
JNI is used for native code to communicate with Java within the same process.
- 01-03-2011, 09:26 AM #9
Member
- Join Date
- Dec 2010
- Posts
- 9
- Rep Power
- 0
Thank you toadaly
Is there any way except pipes(You told that JNI would not work for this type of problem), where a c++ function should call java program and then both should communicate till the end?
- 01-03-2011, 10:16 AM #10
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,385
- Blog Entries
- 7
- Rep Power
- 17
If you start the JVM from your C++ program not using that system( ... ) call both your C++ code and the JVM run in the same process space. Your C++ code can control what the JVM does (run a method, create an object etc.) The JNI manual describes how to do it.
kind regards,
JosWhen people rob a bank they get a penalty; when banks rob people they get a bonus.
- 01-03-2011, 12:35 PM #11
Member
- Join Date
- Dec 2010
- Posts
- 9
- Rep Power
- 0
@Jos
Any way i need to call JAVA program from C++ and then pass the records to java and then resultant records back again and vice versa for all the records.
I think creating JVM instance in C++ and controlling doesn't work.
toadaly said that no way you can communicate when you use previous method that i posted. Pipes implementation is finished but now i want to implement in JNI so now what to do with using fork() so that both programs can exchange information (In any method proposed java program should be called in C++ only no exception for that).
- 01-03-2011, 03:31 PM #12
Senior Member
- Join Date
- Jan 2009
- Posts
- 671
- Rep Power
- 5
If you're willing to use JNI, then I don't understand why you're also trying to spawn a new process. You should probably pick either to spawn a new process, or to use JNI, but not both.
If you do want to use the spawning approach, the there are several common ways to communicate other than pipes (...not sure why you wouldn't want to use pipes, but whatever). You could use the file system, or sockets, or CORBA. If you just really want to combine JNI and multiple processes, then you could write JNI code to access shared memory and communicate that way with the native code.
- 01-04-2011, 12:51 PM #13
Member
- Join Date
- Dec 2010
- Posts
- 9
- Rep Power
- 0
why i am creating child process using fork() is so that both can run at a time and can communicate( i dont know another way to call from c++ and after that, java and c++ communicates in full duplex manner) and one more thing is we used named pipe and got the result and now we need to implement with JNI concept so that both pipe implementation and JNI implementation performance can be compared thats why i am trying to implement with JNI now. If with out using fork() also we can do the same then please give me some suggestions ( JNI is must for me).
- 01-04-2011, 03:22 PM #14
Senior Member
- Join Date
- Jan 2009
- Posts
- 671
- Rep Power
- 5
Similar Threads
-
serial port communication in java
By elsanthosh in forum New To JavaReplies: 2Last Post: 04-06-2010, 06:33 PM -
Java Servlet and Web Services Communication
By kiran.doddi in forum Java ServletReplies: 0Last Post: 03-16-2010, 07:36 PM -
Java Servlet and Web Service communication
By shussain@del.aithent.com in forum Java ServletReplies: 36Last Post: 03-16-2010, 07:28 PM -
java based implementation on serial communication
By xll in forum Threads and SynchronizationReplies: 1Last Post: 12-08-2009, 07:45 AM -
socket communication between c++/java and sending image
By mdemir10 in forum NetworkingReplies: 2Last Post: 09-10-2009, 02:29 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks