Re: Runtime.getRuntime.exec() without exiting the main program
knguyen wrote:
On Feb 13, 2:18 pm, Daniele Futtorovic
<da.futt.newsLOVELYS...@laposte.net> wrote:
On 2008-02-13 19:16 +0100, Andrea Francia allegedly wrote:
knguyen wrote:
Hi all,
I am having a problem using Runtime.getRuntime.exec(). My codes are as
follow
import java.io.*;
public class Test {
public static void main(String[] args) throws Exception {
Process p = Runtime.getRuntime().exec(aCommand);
while (true) {}
}
}
for some reason, the command is not executed until the program exits.
This causes trouble only in Windows because I tested with Linux and it
works fine.
Thanks you
Try this:
import java.io.*;
public class Test {
public static void main(String[] args) throws Exception {
Process p = Runtime.getRuntime().exec(aCommand);
p.waitFo*t*();
}
}
:-)
<http://java.sun.com/javase/6/docs/api/java/lang/Process.html#waitFor()>
Thank you. I forgot to mention I also tried it but I'd like to have
both running. I am trying to invoke a media player, so I would like my
application to do something else while the media is running.
Then use
import java.io.*;
public class Test {
public static void main(String[] args) throws Exception {
Process p = Runtime.getRuntime().exec(aCommand);
doSomething();
}
}
The problem of while(true) {} is that consumes CPU for do nothing, and
if the scheduling is not preemptive it continue to consume CPU.
Instead of while(true) {} you can use this:
while(true) {
try{
Thread.sleep(10000);
} catch(InterruptedException e) {
}
}