communications between two processes in java
Hi,
I am using Runtime.exec() to execute a java program in a seperate jvm. I want to send a message from the java program which is executed in the seperate jvm to the main program which invokes Runtime.exec().
Could anyone can help me in this regard?
Thanks,
Shashidhar Kotta
Hi,
Runtime.exec() returns a Process object. You have access to three different Streams in this process.
Process p = Runtime.exec("mysubprocess" ) ;
p.getInputStream() returns an InputStream that contains everything that you write into the normal OutputStream of mysubprocess (using System.out.print... functions)
p.getErrorStream() returns an InputStream that contains everything that you write into the error stream within mysubprocess (using System.err.print... functions) .
p.getOutputStream() returns an Outputstream that maps to the standard input stream of mysubprocess.
E.g.
You have a Class called IamCalledViaExec with a main method.
You have a class called IamTheCaller.
Within IamTheCaller you should be able to do something like that :
Process p = Runtime.exec("java IamCalledViaExcec" ) ;
InputStream is = p.getInputStream() ;
If you do a
System.out.println("This is a message from me") ;
within IamCalledViaExec , you should be able to read it from the InputStream is in IamTheCaller.