This week's book giveaway is in the JDBC forum.
We're giving away four copies of Make it so: Java DB Connections & Transactions and have Marcho Behler on-line!
See this thread for details.
The moose likes Threads and Synchronization and the fly likes An urgent problem with Runtime API Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Make it so: Java DB Connections & Transactions this week in the JDBC forum!
JavaRanch » Java Forums » Java » Threads and Synchronization
Bookmark "An urgent problem with Runtime API" Watch "An urgent problem with Runtime API" New topic

An urgent problem with Runtime API


Joined: Sep 04, 2001
Posts: 10
Hi,I want to use to execute another process(

when count<=10,it can run well.Otherwise, when count>10,it can not output "sucessful" and the system can not exit.
[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

Joined: Oct 31, 2001
Posts: 60
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<br />Sun Certified Java Programmer<br />Brainbench Certified for Javascript
Nick Culpepper

Joined: Nov 09, 2001
Posts: 1
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:
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 (( != -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();
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++) {
hope this helps to clear things up.

I agree. Here's the link:
subject: An urgent problem with Runtime API
It's not a secret anymore!