简体   繁体   English

Java捕获进程的所有输出并将其记录到文件中

[英]Java Capture all output from a process and log it to file

I'm trying log all output from an Application in java and for some reason it only capturing the first 2 lines i know the application outputs a lot more than this this is my code 我正在尝试从java中的应用程序记录所有输出,并且由于某种原因它只捕获前两行,我知道应用程序输出的内容比这多得多,这是我的代码

logOut = new BufferedWriter(new FileWriter("WebAdmin.log"));

        ProcessBuilder procBuild = new ProcessBuilder(
            "java", "-Xmx1G", "-Xms512M", "-jar", "TekkitServer\\Tekkit.jar", "nogui", "-nojline"
        );
        server = procBuild.start();

        inputStream = new BufferedReader(new InputStreamReader(server.getInputStream()));
        errorStream = new BufferedReader(new InputStreamReader(server.getErrorStream()));
        outputStream = new BufferedWriter(new OutputStreamWriter(server.getOutputStream()));

        String line = "";

        while(!shutdown){
            while((line = inputStream.readLine()) != null){
                logOut.write(line+"\r\n");
                logOut.flush();
                System.out.println(line);
            }
            System.out.println("checking error stream");
            while((line = errorStream.readLine()) != null){
                logOut.write(line+"\r\n");
                logOut.flush();
                System.out.println(line);
            }
        }

        System.out.println("Stoped Reading");
        logOut.close();
        server.destroy();

I'm not even seeing "checking error stream" in my console. 我甚至没有在我的控制台中看到“检查错误流”。

I don't think there is a need for outer while, reading from the output stream blocks until there is some data available, and if the sub process ends you'll get a null and the inner while will break. 我认为不需要外部,从输出流块读取,直到有一些数据可用,如果子进程结束,你将得到一个null ,内部while将会中断。 Also you could use ProcessBuilder 's redirectErrorStream(true) to redirect stderr to stdout so you will catch them both in only one loop. 你也可以使用ProcessBuilderredirectErrorStream(true)stderr重定向到stdout这样你只需要在一个循环中捕获它们。

You must read the stdout and stderr streams in its own thread. 您必须在自己的线程中读取stdout和stderr流。 Otherwise you will get all kinds of blocking situations (depending on operating system and output patterns). 否则,您将获得各种阻塞情况(取决于操作系统和输出模式)。

If you merge the error stream with the output stream ( redirectErrorStream(true) ), you can get along with a single (additional) background thread. 如果将错误流与输出流( redirectErrorStream(true) )合并,则可以使用单个(附加)后台线程。 You can avoid this, when you use redirectOutput(File) (or inheritIO). 使用redirectOutput(File) (或inheritIO)时,可以避免这种情况。

The first two lines in a minecraft server go to STDOUT (tested on bukkit, tekkit, and vanilla), but the rest go to STDERR for what ever reason. Minecraft服务器中的前两行是STDOUT(在bukkit,tekkit和vanilla上测试过),但其余的都是STDERR的原因。 Your application isn't doing anything with errorStream . 您的应用程序没有对errorStream执行任何操作。 You can try using redirection functionality by doing this:. 您可以尝试使用重定向功能:

ProcessBuilder procBuild = new ProcessBuilder(
        "java", "-Xmx1G", "-Xms512M", "-jar", "TekkitServer\\Tekkit.jar",
        "nogui", "-nojline", "2>&1");

Let me know if this works, as this may be a bash-only trick. 让我知道这是否有效,因为这可能只是一个打击伎俩。

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

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