我试图在java中启动一个进程,读取输出,写入程序,然后读取它响应的内容。从SO的所有其他答案来看,这就是我提出的:
class Main
{
public static void main(String[] args) {
String line = "";
try {
ProcessBuilder pb = new ProcessBuilder("C:\\myProgram.exe");
Process p = pb.start();
BufferedReader input = new BufferedReader(new InputStreamReader(p.getInputStream()));
BufferedWriter output = new BufferedWriter(new OutputStreamWriter(p.getOutputStream()));
while ((line = input.readLine()) != null) {
System.out.println(line);
}
output.write("foo");
output.newLine();
output.flush();
while ((line = input.readLine()) != null) {
System.out.println(line);
}
p.destroy();
}
catch (IOException e){
}
}
}
它启动程序,并按预期给我输出。
当我写foo
时,我希望程序能够回复另一个回复,但它永远不会回复。
我做错了什么?