0

Here's my code:

    Runtime re = Runtime.getRuntime();
    BufferedReader output = null;    

    try{
            Process cmd = re.exec("java -jar myProg.jar " + myArgument); 
            output =  new BufferedReader(new InputStreamReader(cmd.getInputStream()));
        }

    catch (Exception e){
            e.printStackTrace();
    }

   String line;
   while ((line = output.readLine()) != null)
   {
    //process line
   }

When debugging this code snippet, I find that when reading each line from output, it skips certain lines.

If i run this myProg.jar from command line, the text that's seen on my command line is not 100% the same as what I get when I process the output from inside my java program!

What could cause this? The output is all text.

Saobi
  • 16,121
  • 29
  • 71
  • 81
  • This doesn't answer your question directly, but instead of using a separate process, have you considered loading the class using a ClassLoader: http://stackoverflow.com/questions/194698/how-to-load-a-jar-file-at-runtime - it might make things easier for you as you don't have to go via a command line - you can communicate with objects. – Mark Byers Nov 26 '09 at 22:45

1 Answers1

3

You only appear to be reading standard out, whereas you may be getting output on standard error as well. I would read both.

Note that you need to read both streams concurrently, to avoid blocking. See this answer for more details.

Community
  • 1
  • 1
Brian Agnew
  • 268,207
  • 37
  • 334
  • 440
  • Why would output be just randomly come out of standard error? doesnt' make sense to me. It's not an error, it's an output. – Saobi Nov 27 '09 at 01:46