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

Output of Process through runtime.exec  RSS feed

 
S Varana
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello everybody,
I am trying to execute C executables in a Java environment. I am able to launch the subprocesses but am unable to print their output simultaneously to the console. I start the process using runtime.exec() and then get the
InputStream of the process with the Process.getInputStream() functions.
Then I spawn a thread to parallely print the contents of the InputStream to the console as the subprocess is executing. However, the output of the C executable is printed to the console only after the entire process is completed. Can Anyone please tell me why this happens ? Replies are appreciated. email id is s_var_2003@yahoo.com
Thanks...
Here is the first part of the code to start the process
try{
proc = r.exec(procString);// procstring is "test.exe"
// output thread prints the ooutput of proc
OutputManager output = new OutputManager(proc.getInputStream());
output.start();
proc.waitFor();
}
catch(Exception e)
{
}
Here is the OutputMAnager class
class OutputManager extends Thread{
DataInputStream dis;
InputStream is ;
Process myP;
Reader r;
BufferedReader br;
public OutputManager(InputStream is){
this.is =is;
}

public void run(){
try{

System.out.println("In run of output manager");
printOutput();
}
catch(Exception e){
e.printStackTrace();
}

}

void printOutput(){
try
{

InputStream inpt = is;//myP.getInputStream();
br = new BufferedReader(new InputStreamReader(inpt));
String outP="";
while( (outP = br.readLine()) != null )
{
System.out.println(outP);
}
}
catch(IOException e)
{
e.printStackTrace();
}

}
 
Joe Ess
Bartender
Posts: 9406
12
Linux Mac OS X Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Does the test.exe process max out the CPU? If so, the java virtual machine has to wait until it finishes before it can get CPU time to process the output.
 
S Varana
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the reply Joe !! The C executable - test.exe is not a very CPU intensive program.
Here is the C code
int main()
{
int i =0;
printf("going to sleep\n");
Sleep(100000);
printf("finished sleep\n");
exit(1);
}

I dont see the first printf's output till the whole program is done.
Thanks...
 
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
The C program doesn't flush its standard output after the first print, so unless the buffer fills up (which it won't since there's so little printed) you won't see the output until more is printed, or until the program ends.
Console drivers act differently -- they're generally line buffered by default.
So it's the C program's fault -- add an fflush(stdout) (I believe that's correct) after the first print and it should behave as you expect.
 
S Varana
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you Earnest. I did add the flush in the C code and it works. However, I would need to run third party C executables through my Java code and I dont have control over their prints. Is there any way to control, the bufferSize through Java ? There are functions to do that in C but then, I suspect the buffersize would be restored to default after the C program exits.
 
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
The details are highly dependent on platform and on the details of the runtime library the C program is using. I'm not an expert, but on UNIX, for example, you may be able to work around this problem (assuming you're actually having this problem) by using Runtime.exec() to invoke an interactive subshell and having the subshell invoke the program. If that doesn't work, you could use something like "expect" to attach the program to actual ptys.
On Windows, all bets are off.
 
S Varana
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Ernest...Thats was really helpful.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!