将另一个程序的输出打印到 java 文本区域
我正在使用 Java 创建 GUI。该 GUI 使用 ProcessBuilder 类从命令行启动程序。
有关正在启动的进程的一些信息:从命令行,它创建另一个窗口并将信息打印到该窗口。
在我的 GUI 窗口中,我有一个文本区域,我想将所述输出重定向到其中。我最初打算使用 SwingWorker 对象来不断检查更多输出,而不是阻碍 GUI。为了测试并确保我拥有原始语法(甚至没有将 GUI 引入到事物中),我想我会将辅助进程窗口的输出打印到 System.out。但是,似乎有些问题,因为我可以在辅助进程的窗口中看到输出,但看不到我正在工作的终端。
代码摘录如下:
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();
boolean run 在程序的其他地方定义,当进程 p 退出或强制退出时设置为 false (附加问题:这是一个非常糟糕的主意吗?我觉得这可能是...... )。
有谁知道为什么当我看到它被打印到另一个窗口时我永远不会得到任何输出?最初我的反应是使用 p.getOutputStream() 但 Scanner 不将 outputStream 作为参数。
感谢您抽出时间。
I am creating a GUI using Java. This GUI launches a program from the command line using the ProcessBuilder class.
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. I originally intended to use a SwingWorker object to constantly check for more output and not hold up the 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. 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...).
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.
Thank you for your time.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您还应该扫描 p.getErrorStream() - 某些程序会写入 STDERR,当从命令行运行时,它与 STDOUT 无法区分。使用这两个流通常是一种很好的做法,因为如果其中任何一个没有被使用,都可能导致外部进程挂起。
You should also scan
p.getErrorStream()
- some programs write to STDERR which is indistinguishable from STDOUT when run from the command line. It is generally good practice to consume both streams, as if either one is not consumed it can cause the external process to hang.如果外部进程将其输出写入其自己的窗口,则几乎可以肯定输出不会写入 STDOUT,这是您用代码读取的内容。如果这样做,那么外部程序的输出将同时出现在其窗口和启动它的命令行会话中(如果存在)。如果无法访问外部程序的源代码,您就不可能拦截其输出,除非作者对该功能做了规定(即将输出重定向到 STDOUT 而不是窗口的命令行开关)。
至于
p.getOutputStream()
,它返回一个从您的角度来看是“输出”的流——即您写入它以将数据发送到进程的STDIN。对于外部程序写入其 STDOUT 的情况,您使用p.getInputStream()
是正确的。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. 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 (i.e. a command-line switch that redirects output to STDOUT instead of the window).
As to
p.getOutputStream()
, that returns a stream which is "output" from YOUR point of view -- i.e. you write to it to send data to the process' STDIN. Your use ofp.getInputStream()
would be correct for the case where the external program writes to its STDOUT.