Log4j 挂起我的应用程序

发布于 2024-10-02 21:37:28 字数 1016 浏览 0 评论 0原文

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

正在配置日志记录...

从以下位置配置 log4j:C:\GPAT\log4j.cfg

并且没有任何反应。我在网上查了一下,看来可能是线程的问题。但我无法修改这个外部应用程序,我必须通过 log4j。我这样读取外部应用程序:

StringBuffer 输出 = 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();
    }

感谢您的阅读。

Im trying to get console output from an external application in my application. When i call the externall app from my code it hangs with the message:

Configuring logging...

Configuring log4j from: C:\GPAT\log4j.cfg

and nothing happens. I searched through internet and it seems that it might be thread issue. But i cant modify this external application and i must go through log4j. I read the external app like this:


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();
    }

Thanks for reading.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

看春风乍起 2024-10-09 21:37:28

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

请参阅此答案的第二段了解更多详细信息和链接到合适的修复程序。

You need to consume both stdout and stderr from a spawned process in separate threads, to prevent blocking behaviour (your spawned process will write to buffers, and block if those buffers aren't being emptied by your consuming process).

See the 2nd paragraph of this answer for more details and a link to a suitable fix.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文