如何在外部程序中处理不同的stdout行为? [英] How to handle different stdout behaviour in external program?

查看:124
本文介绍了如何在外部程序中处理不同的stdout行为?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从Java程序执行外部程序并读取stdout消息in real time,而不必等待程序退出.但是,我发现在不同的.exe程序中存在不同的stdout行为,并且我不知道如何处理它.

Hi I am trying to execute external program from Java program and read the stdout message in real time, without waiting for the program to exit. However, i found that there are different stdout behaviour in different .exe program, and I don't know how to handle it.

示例1:
server1.exe是一个控制台程序.当我运行它时,它将持续监听端口.当客户端连接到它时,它将每1秒生成1行stdout输出.除非我按"ctrl-C",否则它不会退出.

Example 1:
server1.exe is a console program. When i run it, it will continuously listening on a port. When a client is connected to it, it will generate 1 line of stdout output every 1 second. It will not exit unless i press "ctrl-C".

在命令提示符下,运行以下命令:

In a command prompt, I run this:

server1.exe > stdout.out 2> stderr.err

当客户端连接到它时,我发现stdout.out file将实时更新.即使server1.exe仍在运行,我仍可以打开stdout.out file并实时读取stdout输出.

When client is connected to it, I found that stdout.out file will be updated in real time. Even though server1.exe is still running, I can open stdout.out file and read the stdout output in real time.

示例2:
与server1.exe类似,server2.exe也是一个控制台程序.当我运行它时,它还将连续监听端口.当客户端连接到它时,它将每1秒生成1行标准输出输出.除非我按"ctrl-C",否则它不会退出.

Example 2:
Similar to server1.exe, server2.exe is also a console program. When i run it, it will also continuously listening on a port. When client is connected to it, it will generate 1 line of stdout output every 1 second. It will not exit unless i press "ctrl-C".

在命令提示符下,运行以下命令:

In a command prompt, I run this:

server2.exe > stdout.out 2> stderr.err

即使客户端已连接到server2.exe,我也发现stdout.out file为空.只要server2.exe仍在运行,就不会将stdout写入stdout.out file.该文件未在real time中更新.当我按ctrl-C时,它突然将很多行输出写入stdout.out file.

Even though client has connected to server2.exe, I found that stdout.out file is empty. As long as server2.exe is still running, no stdout is written to stdout.out file. That file is not updated in real time. When i press ctrl-C, it suddenly write many lines of output to stdout.out file.

假设我在t = 11按下ctrl-C,它将把从t = 1直到t = 11的所有stdout输出写入stdout.out file.在此之前,t = 10时,stdout.out文件为空.

Assuming that i press ctrl-C at t=11, it will write all stdout output from t=1 until t=11 into the stdout.out file. Before this, at t=10, the stdout.out file is empty.


示例2中的程序给我带来了问题,因为我无法在Java程序中使用read the stdout in real time.我的java程序如下:


The program in example 2 is giving me problem because I am unable to read the stdout in real time in my Java program. My java program is as below:

process = Runtime.getRuntime().exec(command);

input = new BufferedReader(new InputStreamReader(process.getInputStream()));

String inputtext = null;

while ((inputtext = input.readLine()) != null)
{
    //print out the text in Real Time, when the .exe program is still running
}

我是否可以知道示例2中的程序为什么不按ctrl-C键就不会生成stdout output?

May i know why the program in example 2 will not generate stdout output unless I press ctrl-C?

奇怪的是,当我在控制台窗口中手动运行该程序时,我可以看到每1秒钟在控制台窗口上打印一次stdout输出.但是,当我尝试使用inputtext = input.readLine()从Java读取它时,只要程序仍在运行,inputtext就会是null(我已经通过打印出inputtext对其进行了测试).当我按ctrl-C键时,BufferedReader会突然被所有待处理的stdout输出填充.

The strange thing is, when i run that program in console window manually, I can see the stdout output printed on the console window every 1 second. But when I try to read it from Java using inputtext = input.readLine(), inputtext will be null as long as the program is still running (I have tested it by printing out inputtext). When I press ctrl-C, the BufferedReader will suddenly be filled with all the pending stdout output.

如何实时读取server2.exe的stdout?

How can I read stdout of server2.exe in real time?

推荐答案

您描述事物的方式在第二台服务器中发生了一些缓冲.除非服务器连接到实时交互式控制台窗口,否则服务器可能会决定在内部缓冲输出.

The way you describe things, there is some buffering happening in your second server. The server might decide to buffer output internally, unless it is connected to a live interactive console window.

虽然可能有解决此问题的方法,但我会在server2源代码中解决此问题.每当该应用程序每秒写入一次输出时,它应该之后刷新其输出流.也许有一些选择可以启用该行为.如果没有,并且该程序的源代码不在您的控制范围内,请要求开发人员添加刷新功能,以实现更好的集成.

While there may be ways to work around this, I would address this in the server2 source code. Whenever that application writes its once-per-second output, it should flush its output streams afterwards. Perhaps there is some option to enable that behaviour. If there isn't, and if the sources of that program are outside your control, kindly ask the developers to add flushing, in order to allow for better integration.

这篇关于如何在外部程序中处理不同的stdout行为?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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