Interprocess comm using standard input and output
1 posts in topic
Flat View  Flat View
TOPIC ACTIONS:
 

Posted By:   Filip_Panjevic
Posted On:   Sunday, June 11, 2006 09:13 AM

I wish to compile a java test app from my main app and emulate standard input and output for this test application. Code which needs to be compiled looks like this import java.io.*; public class Test { public static void main(String[] args) { try { BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); String str = ""; str = in.readLine(); System.out.println(str); } catch (Throwable t) { t.printStackTrace(); } } } And code used in the main app to run it Process exe_proc = rt.exec("java Test");    More>>

I wish to compile a java test app from my main app and emulate standard input and output for this test application. Code which needs to be compiled looks like this



			
import java.io.*;

public class Test
{
public static void main(String[] args)
{

try {
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
String str = "";

str = in.readLine();
System.out.println(str);

}
catch (Throwable t)
{
t.printStackTrace();
}
}
}


And code used in the main app to run it



			
Process exe_proc = rt.exec("java Test");

OutputStream estdout = exe_proc.getOutputStream();
InputStream estdin = exe_proc.getInputStream();
InputStream eerr = exe_proc.getErrorStream();

BufferedReader pin = new BufferedReader(new InputStreamReader(estdin));
BufferedWriter pout = new BufferedWriter(new OutputStreamWriter(estdout));
BufferedReader perr = new BufferedReader(new InputStreamReader(eerr));

pout.write("Message text");
pout.newLine();

String currentLine = null;
int currentChar = 0;

while (!pin.ready());

while ((currentLine = pin.readLine()) != null) mWind.printlnMessage(currentLine);

exitVal = exe_proc.waitFor();


Main app never gets past while (!pin.ready()). When I try just to read data which Test writes to stdout, it works just fine, but read and write at the same time don't. Help!    <<Less

Re: Interprocess comm using standard input and output

Posted By:   Robert_Lybarger  
Posted On:   Sunday, June 11, 2006 12:19 PM

I haven't read your code in detail; I just skimmed through it. One thing that might be causing problems is you may need to explicitly "flush()" your output stream before trying to read from the input stream ... most especially since you are using the buffered versions of the readers and writers in the test app (the second code you posted). If that solves the problem, its a common thing to neglect.
About | Sitemap | Contact