Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

reading from system command  RSS feed

 
Maha Hassan
Ranch Hand
Posts: 133
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i am using this program to open a unix shell and read the output of the command ..
but it prints all the output after the command finishes ..is there a way to print the output line by line as soon it appears not to wait to the whole command to finish
thanks in advance
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24215
37
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
We can't tell you what you're doing wrong if you don't show us what you're doing!
 
Maha Hassan
Ranch Hand
Posts: 133
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i am trying to use the code (posted on http://java.sun.com/developer/JDCTechTips/2003/tt0304.html )
but i want to print the output line by line as soon as it appears not after executin the comand


Runtime runtime = Runtime.getRuntime();
Process proc = runtime.exec(
"ls" + " " + dir);

// put a BufferedReader on the ls output

InputStream inputstream =
proc.getInputStream();
InputStreamReader inputstreamreader =
new InputStreamReader(inputstream);
BufferedReader bufferedreader =
new BufferedReader(inputstreamreader);

// read the ls output

String line;
while ((line = bufferedreader.readLine())
!= null) {
System.out.println(line);
}

// check for ls failure

try {
if (proc.waitFor() != 0) {
System.err.println("exit value = " +
proc.exitValue());
}
}
catch (InterruptedException e) {
System.err.println(e);
}
}
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I've only done this once (on Windows) and using two threads I noticed the stream reader ran long after the waitFor() returned. Ok, long means a second or so. Is that a big problem?
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!