4

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

jencoston
  • 1,262
  • 7
  • 19
  • 35
Mike
  • 3,515
  • 10
  • 44
  • 67

2 Answers2

4

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());
Joonas Pulakka
  • 36,252
  • 29
  • 106
  • 169
2

Sounds like you need to use a DefaultExecuteResultHandler.

See the tutorial (bottom of) for more info.

Brian Agnew
  • 268,207
  • 37
  • 334
  • 440
  • 1
    well i modified the tutorial from apache with setting up a watchdog for say 3 secs. the program will execute another java application which is a thread that will stop running after say 10 sec. the problem is, the parent won't close until the sub-process closes in 10 secs. the watch dog doesn't work. the parent program is asynchronous.any further help,.? thanks. – simaremare Jan 31 '13 at 07:27
  • I see the same behavior on Linux when trying to run `*.sh` file (the first line is `#!/bin/sh`). Watchdog just doesn't stop this process after timeout! :( – Ruslan Stelmachenko Jun 20 '16 at 02:17