首页 > 解决方案 > 如何从 CMD 读取所有输出以获取日志

问题描述

我不知道如何进行这项工作,但这是问题所在。我将编写一个程序来执行一系列不同的操作。我计划输出所有信息,包括调试信息和所有操作完成。如果我检测到错误,我想关闭程序,然后读取所有输出并创建一个包含所有输出的文件,以便找出问题所在。我想从命令提示符读取所有输出并将其写入带有日期和时间的日志。请不要将此标记为重复,因为我找不到与此相关的答案的任何类似问题。

标签: javalogging

解决方案


Those are some loose lines relevant to your endeavour:

public class StartProcess {
    public static void main(final String[] args) throws IOException, InterruptedException {
        final Process process = Runtime.getRuntime().exec("the app command");
        final InputStream in = process.getInputStream();
        final OutputStream out = process.getOutputStream();
        final InputStream err = process.getErrorStream();
        process.waitFor(); // wait for termination
        final int exitCode = process.exitValue();
    }
}

However, you should keep in mind to read the input from the streams asynchronously.


推荐阅读