简体   繁体   English

从Java中的Process获取所有输出到控制台

[英]Get all output to console from a Process in java

I'm working on an application to act as a 'wrapper' around another another application, in this case I'm working on a wrapper in java to go around a TF2 server. 我正在一个应用程序上充当另一个应用程序的“包装器”,在这种情况下,我正在Java中的包装器上运行以围绕TF2服务器。 However, I'm running into a rather odd issue with getting the output from the console to appear. 但是,在从控制台显示输出时遇到了一个很奇怪的问题。 Even if I redirect the error stream so that it will go with the input stream, the output to console will cut off after a certain point. 即使我重定向错误流以便与输入流一起使用,控制台的输出也会在特定时间点后切断。 I can connect to the server in TF2, so I know for a fact that it's launched. 我可以在TF2中连接到服务器,因此我知道它已经启动。

The code for the wrapper: 包装程序的代码:

public class TF2Wrapper {

public static void main(String[] args) throws IOException {
    ProcessBuilder process = new ProcessBuilder("./srcds_run","+map pl_badwater","+port 27015","2>&1");
    process.redirectErrorStream(true);
    Process server = process.start();
    BufferedReader inputStream = new BufferedReader(new InputStreamReader(server.getInputStream()));
    String s;
    while((s = inputStream.readLine()) != null)
    {
        System.out.println(s);
    }
}

It will read the output up until the second time this line appears: 'Calling BreakpadMiniDumpSystemInit',then no further console output appears, or atleast is picked up by the application. 它将一直读取输出,直到第二次出现此行:'Calling BreakpadMiniDumpSystemInit',然后不再显示任何控制台输出,或由应用程序至少拾取。

Is there anything I can do to fix this or is that not possible? 我能做些什么来解决这个问题还是不可能?

Edit: My guess is it has something to do with the buffering, as trying to do this with python works fine. 编辑:我的猜测是它与缓冲有关,因为尝试使用python可以正常工作。

Instead of using BufferedReader use ByteArrayOutputStream 而不是使用BufferedReader,请使用ByteArrayOutputStream

    public class Example {
        public static void main(String[] args) throws IOException,
                InterruptedException {
            InputStream inputStream = null;
            ByteArrayOutputStream arrayOutputStream = null;
//in case of window
            ProcessBuilder builder = new ProcessBuilder("ipconfig");

            try {
                Process process = builder.start();
                inputStream = process.getInputStream();
                byte[] b = new byte[1024];
                int size = 0;
                arrayOutputStream = new ByteArrayOutputStream();

                while ((size = inputStream.read(b)) != -1) {
                    arrayOutputStream.write(b, 0, size);
                }

                System.out.println(new String(arrayOutputStream.toByteArray()));

            } catch (Exception e) {
                e.getStackTrace();
            } finally {
                try {
                    if (inputStream != null)
                        inputStream.close();
                    if (arrayOutputStream != null)
                        arrayOutputStream.close();
                } catch (Exception exception) {
                    exception.getStackTrace();
                }
            }
    }
    }

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

相关问题 我无法在FOP流程中捕获Java的控制台输出,并且我的所有流程均无法正常运行 - I can't capture the console output from Java in FOP process and all my process it doesn't work Java捕获进程的所有输出并将其记录到文件中 - Java Capture all output from a process and log it to file 如何从控制台应用程序获取实时Java输出 - How to get live java output from console application 如何从oracle的Java代码获取输出到oracle控制台? - How to get output into oracle console from java code of oracle? 使用java获取进程的输出结果 - Get the output result of a process with java java新过程-从ErrorStreams和Output流获取和读取它是必需的 - java new process - is necessary to get and read from ErrorStreams and Output streams 从Java进程获取输出 - Getting Output from the java process 如何将Java所有行从java控制台输出保存到文本文件并正确附加? - How can I save java all lines from java console output to a text file and properly append it? 从另一个Java进程在Windows控制台中运行Java进程 - Run java process in windows console from another java process 无法从带有 Java 进程的 Docker 控制台分离 - Cannot detach from Docker console with Java process on it
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM