in src/main/java/org/apache/commons/exec/DefaultExecutor.java [329:403]
private int executeInternal(final CommandLine command, final Map<String, String> environment, final Path workingDirectory,
final ExecuteStreamHandler streams) throws IOException {
final Process process;
exceptionCaught = null;
try {
process = launch(command, environment, workingDirectory);
} catch (final IOException e) {
if (watchdog != null) {
watchdog.failedToStart(e);
}
throw e;
}
try {
setStreams(streams, process);
} catch (final IOException e) {
process.destroy();
if (watchdog != null) {
watchdog.failedToStart(e);
}
throw e;
}
streams.start();
try {
// add the process to the list of those to destroy if the VM exits
if (getProcessDestroyer() != null) {
getProcessDestroyer().add(process);
}
// associate the watchdog with the newly created process
if (watchdog != null) {
watchdog.start(process);
}
int exitValue = INVALID_EXITVALUE;
try {
exitValue = process.waitFor();
} catch (final InterruptedException e) {
process.destroy();
} finally {
// see https://bugs.sun.com/view_bug.do?bug_id=6420270
// see https://issues.apache.org/jira/browse/EXEC-46
// Process.waitFor should clear interrupt status when throwing InterruptedException
// but we have to do that manually
Thread.interrupted();
}
if (watchdog != null) {
watchdog.stop();
}
try {
streams.stop();
} catch (final IOException e) {
setExceptionCaught(e);
}
closeProcessStreams(process);
if (getExceptionCaught() != null) {
throw getExceptionCaught();
}
if (watchdog != null) {
try {
watchdog.checkException();
} catch (final IOException e) {
throw e;
} catch (final Exception e) {
throw new IOException(e);
}
}
if (isFailure(exitValue)) {
throw new ExecuteException("Process exited with an error: " + exitValue, exitValue);
}
return exitValue;
} finally {
// remove the process to the list of those to destroy if the VM exits
if (getProcessDestroyer() != null) {
getProcessDestroyer().remove(process);
}
}
}