java - 捕获java的输出

标签 java process stdout output

我正在 try catch java 应用程序的输出,但我遗漏了一些东西。我正在使用以下批处理文件:

@echo off
echo Start of batch
java makeQRCode
echo End of batch

如果我从命令提示符运行批处理文件,我会看到我期望的 3 行。

Start of batch
Here is your java output.
End of batch

当我使用以下代码调用批处理文件时,我只看到第一行和最后一行,但看不到中间的行。

try{
    Runtime rt = Runtime.getRuntime();
    log.debug("Calling rt.exec...");
    Process pr = rt.exec("c:\\adhoc\\java\\qrcode\\makeQRCode.bat");

    BufferedReader input = new BufferedReader(new InputStreamReader(pr.getInputStream()));
    String line=null;
    while((line=input.readLine()) != null) {
        out.print(line);
        log.debug(line);
    }
    input.close();

    int exitVal = pr.waitFor();
}catch (Exception e){
    log.error("Error generating qr code: " + e.toString());
}       

这是运行上述代码时我的日志输出:

2013-05-23 17:16:32,957 DEBUG _getimage - Start of batch
2013-05-23 17:16:33,126 DEBUG _getimage - End of batch

我想我错过了一些与标准输出相关的东西,但我已经玩了几个小时了,只是无法得到正确的结果。

谢谢

最佳答案

查找 java 二进制文件或查找 makeQRCode 类可能存在问题。检查Process的错误流。

BufferedReader error = new BufferedReader(new InputStreamReader(pr.getErrorStream()));
String errorLine = null;
while ((errorLine = input.readLine()) != null) {
   System.out.print(errorLine);
}

关于java - 捕获java的输出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16724573/

相关文章:

node.js - FFMPEG 和 Node.js Spawn 不输出任何内容

python - 如何在 Python 中禁用 socketserver 的输出?

c - 为什么不执行子进程?

c语言,标准输出中的额外消息?

java - 如何计算百分比

java - 斯坦福CoreNLP NoSuchMethodError

java - 如何在 JavaFX 中设置滚动 Pane 的单位增量?

java - 使用 Glassfish 的 Ldap 基本身份验证

c# - Process.Kill() 与 Process.Start ("taskkill",...)

c - 如何仅计算当前进程(包括所有线程)的时钟周期