Results 1 to 14 of 14
  1. #1
    mahesh.gmb is offline Member
    Join Date
    Dec 2010
    Posts
    9
    Rep Power
    0

    Default 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.

  2. #2
    mahesh.gmb is offline Member
    Join Date
    Dec 2010
    Posts
    9
    Rep Power
    0

    Default

    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.

  3. #3
    mahesh.gmb is offline Member
    Join Date
    Dec 2010
    Posts
    9
    Rep Power
    0

    Default

    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

  4. #4
    toadaly is offline Senior Member
    Join Date
    Jan 2009
    Posts
    671
    Rep Power
    6

    Default

    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.

  5. #5
    mahesh.gmb is offline Member
    Join Date
    Dec 2010
    Posts
    9
    Rep Power
    0

    Default

    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

  6. #6
    toadaly is offline Senior Member
    Join Date
    Jan 2009
    Posts
    671
    Rep Power
    6

    Default

    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 04:48 PM.

  7. #7
    mahesh.gmb is offline Member
    Join Date
    Dec 2010
    Posts
    9
    Rep Power
    0

    Default

    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?

  8. #8
    toadaly is offline Senior Member
    Join Date
    Jan 2009
    Posts
    671
    Rep Power
    6

    Default

    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.

  9. #9
    mahesh.gmb is offline Member
    Join Date
    Dec 2010
    Posts
    9
    Rep Power
    0

    Default

    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?

  10. #10
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,783
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by mahesh.gmb View Post
    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?
    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,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  11. #11
    mahesh.gmb is offline Member
    Join Date
    Dec 2010
    Posts
    9
    Rep Power
    0

    Default

    @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).

  12. #12
    toadaly is offline Senior Member
    Join Date
    Jan 2009
    Posts
    671
    Rep Power
    6

    Default

    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.

  13. #13
    mahesh.gmb is offline Member
    Join Date
    Dec 2010
    Posts
    9
    Rep Power
    0

    Default

    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).

  14. #14
    toadaly is offline Senior Member
    Join Date
    Jan 2009
    Posts
    671
    Rep Power
    6

    Default

    If you want to use JNI and have parallel execution, you need to use threads instead of fork.

Similar Threads

  1. serial port communication in java
    By elsanthosh in forum New To Java
    Replies: 2
    Last Post: 04-06-2010, 07:33 PM
  2. Java Servlet and Web Services Communication
    By kiran.doddi in forum Java Servlet
    Replies: 0
    Last Post: 03-16-2010, 08:36 PM
  3. Java Servlet and Web Service communication
    By shussain@del.aithent.com in forum Java Servlet
    Replies: 36
    Last Post: 03-16-2010, 08:28 PM
  4. java based implementation on serial communication
    By xll in forum Threads and Synchronization
    Replies: 1
    Last Post: 12-08-2009, 08:45 AM
  5. Replies: 2
    Last Post: 09-10-2009, 03:29 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •