在Java代码里调用外部程序时,比如一个ksh等,可以用如下方法:
The ProcessBuilder.start()
and Runtime.exec
methods create a native process and return an instance of a subclass of Process
that can be used to control the process and obtain information about it.
所以在调用外部程序时,使用process将启动一个新进程,用于执行外部程序。使用waitfor()等待外部程序执行完毕并返回值。
String command = String.format(CHECK_GRAPH_IS_RUNNING_FORMAT, recoveryFile.getAbsolutePath());
Process process = execCommand(command);
int isRunningExitCode = process.waitFor();
return (SUCCESS_EXIT_VALUE == isRunningExitCode);
protected Process execCommand(final String command) throws IOException {
return Runtime.getRuntime().exec(command, getEnvp(), getWorkingFolder());