Mike Mike - 2 months ago 19
Java Question

How to start a program and exit immediately using apache commons-exec?

By using Apache Commons-Exec I can start a program successfully, however the starter program suspended after the program started.

May I ask how to start a program and exit immediately so it will not block the following execution?

DefaultExecutor executor = new DefaultExecutor();
executor.execute( "cmd /c start C:\\Users\\xx\\program.exe");


I'm on Win7 64bit.

Thanks

Answer

According to documentation, execute(CommandLine) starts synchronous execution. That is, it blocks the calling thread. You probably want asynchronous execution, so use execute(CommandLine command, ExecuteResultHandler handler). For example,

DefaultExecutor executor = new DefaultExecutor();
executor.execute(new CommadLine("cmd /c start C:\\Users\\xx\\program.exe"),
                 new DefaultExecuteResultHandler());
Comments