阻塞执行方式:
@Slf4j
public class CmdUtils {
/**
* 执行系统命令, 返回执行结果
*
* @param cmd 需要执行的命令
* @param distPath 执行命令的子进程的工作目录, null 表示和当前主进程工作目录相同
*/
public static String execCmd(String cmd, String distPath) {
File file = new File(distPath);
int exitValue = 1;
try {
// 执行命令, 返回一个子进程对象(命令在子进程中执行)
log.info("开始执行命令和目录:[{}],[{}]" , cmd,distPath);
CommandLine cmdLine = CommandLine.parse(cmd);
DefaultExecutor executor = new DefaultExecutor();
executor.setWorkingDirectory(file);
executor.setExitValues(null);
ExecuteWatchdog watchdog = new ExecuteWatchdog(600000);
executor.setWatchdog(watchdog);
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
ByteArrayOutputStream errorStream = new ByteArrayOutputStream();
PumpStreamHandler streamHandler = new PumpStreamHandler(outputStream, errorStream);
executor.setStreamHandler(streamHandler);
exitValue = executor.execute(cmdLine);
//获取程序外部程序执行结果
String out = outputStream.toString("utf8");
String error = errorStream.toString("utf8");
log.info("执行命令处理结果exitValue:[{}],out:[{}],error:[{}]",exitValue,out,error);
} catch (Exception e) {
log.error("执行批处理命令异常[{}]", e);
throw new BusinessException(RetCode.CMD_ERROR);
}
log.info("结束执行命令和目录:[{}],[{}][{}s]" , cmd,distPath);
if(exitValue != 0){
log.error("执行批处理命令失败");
throw new BusinessException(RetCode.CMD_ERROR);
}
return String.valueOf(exitValue);
}
如果不想在执行外部命令的时候,把当前线程阻塞,可以使用DefaultExecuteResultHandler处理外部命令执行的结果,释放当前线程。
@Slf4j
public class CmdUtils {
/**
* 执行系统命令, 返回执行结果
*
* @param cmd 需要执行的命令
* @param distPath 执行命令的子进程的工作目录, null 表示和当前主进程工作目录相同
*/
public static boolean execCmd(String cmd, String distPath) {
File file = new File(distPath);
boolean result = true;
if(!file.exists()){
file.mkdir();
}
int exitValue = 1;
try {
// 执行命令, 返回一个子进程对象(命令在子进程中执行)
log.info("开始执行命令和目录:[{}],[{}]" , cmd,distPath);
CommandLine cmdLine = CommandLine.parse(cmd);
DefaultExecutor executor = new DefaultExecutor();
executor.setWorkingDirectory(file);
executor.setExitValues(null);
ExecuteWatchdog watchdog = new ExecuteWatchdog(600000);
executor.setWatchdog(watchdog);
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
ByteArrayOutputStream errorStream = new ByteArrayOutputStream();
PumpStreamHandler streamHandler = new PumpStreamHandler(outputStream, errorStream);
executor.setStreamHandler(streamHandler);
//非阻塞
DefaultExecuteResultHandler resultHandler = new DefaultExecuteResultHandler();
try {
executor.execute(cmdLine, resultHandler);
resultHandler.waitFor();
} catch (Exception e) {
result = false;
e.printStackTrace();
}
} catch (Exception e) {
log.error("执行批处理命令异常[{}]", e);
throw new BusinessException(RetCode.CMD_ERROR);
}
log.info("结束执行命令和目录:[{}],[{}][{}s]" , cmd,distPath);
if(exitValue != 0){
log.error("执行批处理命令失败");
throw new BusinessException(RetCode.CMD_ERROR);
}
return result;
}