java – 打印运行时exec()OutputStream到控制台
作者:互联网
我试图让exec()启动进程的OutputStream到控制台.如何才能做到这一点?
这是一些不完整的代码:
import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.OutputStream;
import java.io.PrintStream;
import java.io.Reader;
public class RuntimeTests
{
public static void main(String[] args)
{
File path = new File("C:\\Dir\\Dir2");
String command = "cmd /c dir";
Reader rdr = null;
PrintStream prtStrm = System.out;
try
{
Runtime terminal = Runtime.getRuntime();
OutputStream rtm = terminal.exec(command, null, path).getOutputStream();
prtStrm = new PrintStream(rtm);
prtStrm.println();
}
catch (IOException e)
{
e.printStackTrace();
}
}
}
解决方法:
在调用process.waitFor()之后,您需要启动一个新线程来读取终端输出流并将其复制到控制台
标签:runtime-exec,java,stream,runtime,printstream 来源: https://codeday.me/bug/20190916/1806710.html