Waiting a Dos Command?

Hi;

In one of our projects, we have to call a dos command in java. That seems ok however there is a problem about code. It gives an exception because the remaning program goes without waiting the dos command's completion.

Runtime r = Runtime.getRuntime();

r.exec("cmd /C sort 1.txt /O 2.txt");

Above code, makes a sort in 1.txt and saves it to 2.txt.. My code goes without waiting the 2.txt to be created and gives an exception like couldn't found 2.txt . Thanks for any help, i hope i could explained clearly what i wanted to ask..

[592 byte] By [batuja] at [2007-10-2 4:47:14]
# 1
I read somewhere on this forum that you could use the waitFor() method in the Process class (which Runtime.exec() returns) to do something similar to what you want to do.
Learnablea at 2007-7-16 0:52:01 > top of Java-index,Administration Tools,Sun Connection...
# 2

Yea you're right thanks for answer. But yesterday Petter a.k.a wildfrog from Codeguru forums posted this solutions. Anyway i'm putting it here if someone needs it.

[QUOTE=wildfrog]To wait until the 'child process' has completed, you can do something like this:

Runtime r = Runtime.getRuntime();

Process p = r.exec("cmd /C sort 1.txt /O 2.txt");

// wait for it to complete

try

{

p.waitFor();

} catch (InterruptedException e)

{

// wait was interrupted

}

- petter[/QUOTE]

batuja at 2007-7-16 0:52:01 > top of Java-index,Administration Tools,Sun Connection...