创建进程并读取进程的输入流。
(Javadoc中)的getInputStream()
获得子进程的输入流。流获取从此Process对象表示的进程的标准输出流中传送的数据。
//-->check command line<--
Process process = Runtime.getRuntime().exec("/usr/bin/php /home/amit/hello.php");
BufferedInputStream iStream = new BufferedInputStream(process.getInputStream());
BufferedOutputStream oStream = new BufferedOutputStream(process.getOutputStream());
byte[] buffer = new byte[1024];
while (true){
int length = iStream.read(buffer);
if(length == -1)
break;
System.out.println(new String(buffer, 0, length));
}
注:已经写入Linux。
一般提示:1)阅读(并实现)*所有*的建议[当Runtime.exec()不会](http://www.javaworld.com/jw-12-2000/ JW-1229-traps.html)。这可能会解决问题。如果不是,它应该提供更多关于失败原因的信息。然后忽略它引用'exec'并使用'ProcessBuilder'构建'Process'。还要将'String arg'分解为'String [] args'来解释其本身包含空格的参数。 2)提出问题。 –
@AndrewThompson如果脚本要求更多的输入,我会怎么做? – junyi00
不确定那个,但看看'Process'提供的I/O流。 –