This week's book giveaway is in the Other Languages forum.
We're giving away four copies of Functional Reactive Programming and have Stephen Blackheath and Anthony Jones on-line!
See this thread for details.
Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

An urgent problem with Runtime API

 
chenw
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,I want to use Exe.java to execute another process(Erro.java).

when count<=10,it can run well.Otherwise, when count>10,it can not output "sucessful" and the system can not exit.
why?
[Inserted CODE tags and protected < to make code readable - PdH]
[This message has been edited by Peter den Haan (edited November 02, 2001).]
 
Jyothi Lature
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey chenw,
I think if you remove WaitFor() method, the program runs fine.
This method causes the current thread to wait. So, you can as well remove it.
I dont know what is the necessity of it..
Have fun,
Jyothi
 
Nick Culpepper
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
chenw,
the problem with your example code has to do with the output stream of the process your are creating. java does not tie the io streams between parent/child processes for you. you must do this yourself. since buffer sizes can differ on various platforms, if a child process is writing to it's output stream and the parent is not reading from the childs stream, the child process will "lock" when it fills it's output buffer. here is a simple fix to your code:
import java.io.InputStream;
public class Exe {
public static void main(String[] args) throws Exception {
Runtime r=Runtime.getRuntime();
Process p=r.exec("java Erro "+args[0]);
InputStream is = p.getInputStream();
int b;
while ((b=is.read()) != -1) System.out.write(b);
// waitFor() not necessary with current Erro impl
// since the read() blocks, and when the child process
// terminates, the stream will be closed;
// p.waitFor();
System.out.println("sucessful");
System.exit(0);
}
}
public class Erro {
public static void main(String[] args) {
int count=10;
try {
count = Integer.parseInt(args[0]);
} catch (Exception e) { }
for(int i=0;i < count;i++) {
System.out.println("e");
}
}
}
hope this helps to clear things up.
nick

 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic