Communication between a java and c++ program

Hi,

I have the following problem. I have 2 programs, a java program and a c++ program. These 2 programs have to communicate with each other in full duplex mode. I want to use pipes for this communication. So when the c++ program pust something on the stdout the java program must be able to read this and the other way around. Can, or is it wise to use pipes for doing this and if it isn't, what other options do I have?

thx in advance,

lmnt22

Try running the c program over jni (java native interface)
assuming you have the c++ program source code
over jni methods you can put the other language directly into your java program and define interfaces so there is no need to go over stdout etc

-hope this helps- :slight_smile:

From your question, I assume you have 2
separate programs (1 java and 1 c++) and have
the source code for these programs. I also
assume that each of these programs reads stdin
for it's data and writes data to stdout.

I also assume that these programs have no
relationship to each other meaning that one
program does not start the other as a child.

If you want to use pipes, you may want to
consider FIFO devices (sometimes referred to
as a "named" pipe). You can create FIFO's
using the "mknode" command. If you want you
programs to utilize stdin and stdout, for example:

myjavaprog < /dev/myfifo1 > /dev/myfifo2 &
myc++prog < /dev/myfifo2 > /dev/myfifo1

then your code should first close all open
file descriptors and open /dev/myfifo1 and
/dev/myfifo2 for reading or writing as
appropriate. Also, you should open a file
for error output as well. Once you've done this,
the programs should work as if stdin and stdout
have been redirected. The main caveat here is that
you can expierence deadlock if your programs
block on reading or writing as reading and empty
pipe will wait forever and writing a full pipe
will block forever unless you set up your
connections as non-blocking. There are actually
a number of ways to deal with this but it does
require some forthought.

I hope this helps a bit. :slight_smile:

Thx guys,

I discussed both methods with my collegue and we didn't choose any of both methods. One of the issues is that both programs have to be portable to almost any system. We don't realy know if windows pipe handling is the same as unix pipe handling. It pipes isn't an option. (I only found this out after I wrote the topic)

Considering the small amount of time we have to complete the project we decided to implement some messeging system, which is indipendent of the method u use for in/output. For now we use 2 temp files which for each program (there are indeed 2 programs, a java program and a c program). But it might be possible to use pipes or sockets in the future. An advantage is that there doesn't have to be send much data between the 2 programs.(java is the gui and the c++ program handles the openGL drawing and heavy calculations).

So we use temporary files. However I would like to thank you for your fast response!

lmnt22

Portability to Windoze is a very significant
addition to your orignally stated problem
however, you still have some options.
Here's an interesting article on Linux and Win2K
pipes...

http://www-106.ibm.com/developerworks/linux/library/l-rt4/?open&t=grl,l=805,p=pipes

...and from MS...

...it may give you more ideas and options.

Personally, I would recommend sockets.
Enjoy :stuck_out_tongue: