简体   繁体   English

将另一个程序的输出打印到java文本区域

[英]Printing output of another program to a java text area

I am creating a GUI using Java. 我正在使用Java创建GUI。 This GUI launches a program from the command line using the ProcessBuilder class. 此GUI使用ProcessBuilder类从命令行启动程序。

A little information on the process being launched: from the command line, it creates another window and prints information to said window. 有关正在启动的进程的一些信息:从命令行,它创建另一个窗口并将信息打印到所述窗口。

In my GUI window, I have a text area to where I would like to redirect said output. 在我的GUI窗口中,我有一个文本区域,我想重定向所述输出。 I originally intended to use a SwingWorker object to constantly check for more output and not hold up the GUI. 我原本打算使用SwingWorker对象来不断检查更多输出而不是阻止GUI。 To test and make sure I had the original syntax down (without even bringing the GUI into things) I thought I would print the output from the secondary process' window to System.out. 为了测试并确保我原来的语法已经关闭(甚至没有将GUI带入事物中)我想我会将辅助进程窗口的输出打印到System.out。 However, something seems to be wrong as I can see the output in the secondary process' window, but not the terminal from which I am working. 但是,有些东西似乎是错误的,因为我可以在辅助进程的窗口中看到输出,但不是我正在工作的终端。

Excerpt of code is as follows: 代码摘录如下:

Process p = pb.start(); 
Scanner s = new Scanner(p.getInputStream());

SwingWorker pipe = new SwingWorker<String, Void> (){
    public String doInBackground(){
        while(run){
            if(s.hasNextLine()){
                System.out.println("S has next!");
                System.out.println(s.nextLine());
            }
        }
        return null;
    }
};
pipe.execute();

The boolean run is defined elsewhere in the program and is set to false when the process p exits or is force quit (additional question: is that a really bad idea? I feel like it might be...). 布尔运行在程序的其他地方定义,并在进程p退出或强制退出时设置为false(另外一个问题:这是一个非常糟糕的主意吗?我觉得它可能是......)。

Does anyone have an idea as to why I am never getting any output when I see it being printed to the other window? 当我看到它被打印到另一个窗口时,有没有人知道为什么我从来没有得到任何输出? Initially my reaction was to use p.getOutputStream() but Scanner does not take an outputStream as a paramter. 最初我的反应是使用p.getOutputStream()但Scanner不会将outputStream作为参数。

Thank you for your time. 感谢您的时间。

You should also scan p.getErrorStream() - some programs write to STDERR which is indistinguishable from STDOUT when run from the command line. 您还应该扫描p.getErrorStream() - 某些程序写入STDERR,从命令行运行时与STDOUT无法区分。 It is generally good practice to consume both streams, as if either one is not consumed it can cause the external process to hang. 通常很好的做法是同时使用两个流,就好像任何一个流都没有被消耗它可能导致外部进程挂起。

If the external process is writing its output to its own window, it is almost certain that the output is NOT being written to STDOUT, which is what you are reading with your code. 如果外部进程正在将其输出写入其自己的窗口,则几乎可以肯定输出未写入STDOUT,这是您使用代码读取的内容。 If it did so, then the external program's output would be appearing both in its window and in the command line session from which it was launched (if one existed). 如果它这样做,则外部程序的输出将出现在其窗口和启动它的命令行会话中(如果存在)。 Without access to the source of the external program it's unlikely you will be able to intercept its output unless the authors made provisions for that functionality (ie a command-line switch that redirects output to STDOUT instead of the window). 如果不访问外部程序的源代码,除非作者为该功能做出规定(即将输出重定向到STDOUT而不是窗口的命令行开关),否则您不太可能截取其输出。

As to p.getOutputStream() , that returns a stream which is "output" from YOUR point of view -- ie you write to it to send data to the process' STDIN. 至于p.getOutputStream() ,它从你的角度返回一个“输出”的流 - 即你写入它以将数据发送到进程'STDIN。 Your use of p.getInputStream() would be correct for the case where the external program writes to its STDOUT. 对外部程序写入STDOUT的情况,使用p.getInputStream()是正确的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM