Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.java.programmer > #31843

Re: a very basic core Java question for help

From Daniel Pitts <newsgroup.nospam@virtualinfinity.net>
Newsgroups comp.lang.java.programmer
Subject Re: a very basic core Java question for help
References <b5a75413-3461-420e-814a-913a25ad0d54@googlegroups.com>
Message-ID <Yz0ix.55602$XL.16700@fx27.iad> (permalink)
Date 2015-06-22 16:08 -0700

Show all headers | View raw


On 6/11/15 6:57 AM, John wrote:
> Hi:
>
> I bump into this strange issue. My Java code below calls external command(or system command). My understanding is that the Java process will be the parent process and the started new process will be the child process. The tricky part is that how to get the printout from the child process. Due to the relationship of the two processes, the child printout becomes the input to the parent process. My code can get the child process(again, external program) print out. My problem is that I want to these child process output show up IN TIME on the console. When I run the program in Eclipse, the child process (it runs for several minutes) printout indeed show up at the console IN TIME. However, if I run the program through ANT on the command line, those printout lines only show up AFTER the child process is finished. Can you explain to me why running through ANT, those printout from the child process were hold?
>
> Thank you very much.
>
>
>          final ProcessBuilder processBuilder = new ProcessBuilder(args);//args is String[]
>
>          processBuilder.redirectErrorStream(true);
>          //merge stdout and stderr of the external process -- this feature only available for ProcessBuilder, not available for RunTime.getRunTine().exec(..)
>
>          final Process processRunSysCmd = processBuilder.start();//start a new process outside of JVM -- a child process
>
>          //read the external process stdout and stderr
>
>          final BufferedReader stdReader = new BufferedReader(new InputStreamReader(processRunSysCmd.getInputStream()));
>
>          String stdline;
>
>          try
>          {
>              while((stdline = stdReader.readLine()) != null)
>              {//getting the child process printout line by line
>
>                  System.out.println(stdline);//try to print out to the console
>              }
>          }
>          finally
>          {//Should guarantee the reader is closed... put it in a finally.
>
>              stdReader.close();
>          }
>

It's hard to see your exact problem since we don't now your exact 
process and how it handles output.  If it doesn't produce and line 
endings, then readLine() will block until it does.  You may find that 
you'd be better off reading one character at a time and outputting it as 
you go, without using buffered reader.

Back to comp.lang.java.programmer | Previous | Next — Previous in thread | Find similar | Unroll thread


Thread

a very basic core Java question for help John <xsli2@yahoo.com> - 2015-06-11 06:57 -0700
  Re: a very basic core Java question for help Daniel Pitts <newsgroup.nospam@virtualinfinity.net> - 2015-06-22 16:08 -0700

csiph-web