打印运行时exec()OutputStream到控制台 [英] Printing Runtime exec() OutputStream to console

查看:386
本文介绍了打印运行时exec()OutputStream到控制台的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试获取由 exec()发起的进程 OutputStream 到控制台。怎么办呢?

I am trying to get the OutputStream of the Process initiated by exec() to the console. How can this be done?

这里有一些不完整的代码:

Here is some incomplete code:

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()

You need to start a new thread that would read terminal output stream and copy it to the console, after you call process.waitFor()

这篇关于打印运行时exec()OutputStream到控制台的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆