我有以下Java代码来启动ProcessBuilder,打开OutputStream,让进程向OutputStream中写入字符串,然后关闭OutputStream。当我尝试关闭OutputStream时,整个事件会无限期地挂起。这只发生在Windows上,从未在Mac或Linux上发生。Java在试图关闭ProcessBuilder OutputStream时挂起
一些相关的问题似乎接近我遇到的同样的问题,但我一直无法弄清楚如何将答案应用到我的问题中,因为我是一个与Java相关的新手。这是代码。您可以看到我已经放入了很多println语句来尝试隔离问题。
System.out.println("GenMic trying to get the input file now");
System.out.flush();
OutputStream out = child.getOutputStream();
try {
System.out.println("GenMic getting ready to write the input file to out");
System.out.flush();
out.write(intext.getBytes()); // intext is a string previously created
System.out.println("GenMic finished writing to out");
System.out.flush();
out.close();
System.out.println("GenMic closed OutputStream");
System.out.flush();
} catch (IOException iox) {
System.out.println("GenMic caught IOException 2");
System.out.flush();
String detailedMessage = iox.getMessage();
System.out.println("Exception: " + detailedMessage);
System.out.flush();
throw new RuntimeException(iox);
}
这里是在执行这个块的输出:
GenMic试图让输入文件现在
GenMic准备写输入文件出来
GenMic成品写出来