John Sulik
John Sulik

Reputation: 96

Capturing output from java

I'm trying to capture output from a java app but I'm missing something. I'm using the following batch file:

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

If I run the batch file from a command prompt, I see the 3 lines I'm expecting.

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

When I call the batch file using the following code, I only see the first and last lines but not the one in the middle.

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

Here's my log output when the above code is run:

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

I think I'm missing something related to stdout but I've been playing with it for hours now and just can't get it right.

Thanks

Upvotes: 1

Views: 1847

Answers (1)

Reimeus
Reimeus

Reputation: 159874

There may be an issue either finding the java binary or locating the makeQRCode class. Check the error stream of the Process.

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

Upvotes: 2

Related Questions