【问题标题】:Log4j hanging my applicationLog4j 挂起我的应用程序
【发布时间】:2011-05-10 17:56:42
【问题描述】:

我正在尝试从我的应用程序中的外部应用程序获取控制台输出。当我从我的代码中调用外部应用程序时,它会挂起以下消息:

正在配置日志记录...

从 C:\GPAT\log4j.cfg 配置 log4j

然后什么也没有发生。我通过互联网搜索,似乎它可能是线程问题。但我不能修改这个外部应用程序,我必须通过 log4j。我像这样阅读外部应用程序:

StringBuffer output = new StringBuffer();

    try {
        Runtime rt = Runtime.getRuntime();
        Process proc = rt.exec(GSATCommand);
        BufferedReader input = new BufferedReader(new InputStreamReader(proc.getInputStream()));
        System.out.println("Test running...");
        String line = null;
        while ((line = input.readLine()) != null) {
            System.out.println(line);   // Writes the test output to console
            output.append(line);  output.append("\n");
        }
        int exitVal = proc.waitFor();
        System.out.println("Process exitValue: " + exitVal);
        System.out.println("Test successfully executed");
    } catch (Throwable t) {
        t.printStackTrace();
    }

感谢阅读。

【问题讨论】:

    标签: java multithreading log4j


    【解决方案1】:

    您需要在 单独 线程中从衍生进程中同时使用 stdout 和 stderr,以防止阻塞行为(您的衍生进程将写入缓冲区,如果这些缓冲区没有被清空,则阻塞你的消费过程)。

    请参阅this answer 的第 2 段了解更多详细信息和合适修复的链接。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2010-10-12
      • 1970-01-01
      • 1970-01-01
      • 2013-01-16
      • 1970-01-01
      • 1970-01-01
      • 2023-03-20
      • 1970-01-01
      相关资源
      最近更新 更多