wood burning stoves 2.0*
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 OCM Java EE 6 Enterprise Architect Exam Guide this week in the OCMJEA forum!
JavaRanch » Java Forums » Java » Threads and Synchronization
Bookmark "An urgent problem with Runtime API" Watch "An urgent problem with Runtime API" New topic
Author

An urgent problem with Runtime API

chenw
Greenhorn

Joined: Sep 04, 2001
Posts: 10
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

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


Jyothi<br />Sun Certified Java Programmer<br />Brainbench Certified for Javascript
Nick Culpepper
Greenhorn

Joined: Nov 09, 2001
Posts: 1
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

 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: An urgent problem with Runtime API