Run a external application in java but don't wait for it to finish

Daniel Cisek picture Daniel Cisek · Mar 8, 2012 · Viewed 7k times · Source

I'm writing an app in java allowing me to run other applications. To do that, I've used an Process class object, but when I do, the app awaits the process to end before exiting itself. Is there a way to run external application in Java, but don't wait for it to finish?

public static void main(String[] args)
{
FastAppManager appManager = new FastAppManager();
appManager.startFastApp("notepad");
}

public void startFastApp(String name) throws IOException
{
    Process process = new ProcessBuilder(name).start(); 
}

Answer

Esben Skov Pedersen picture Esben Skov Pedersen · Mar 8, 2012

ProcessBuilder.start() does not wait for process to finish. You need to call Process.waitFor() to get that behaviour.

I did a small test with this program

public static void main(String[] args) throws IOException, InterruptedException {
    new ProcessBuilder("notepad").start();
}

When run in netbeans it appear to be still running. When running from command line with java -jar it returns immediately.

So your program is probably not waiting to exit, but your IDE makes it seem so.