| | |
| | | import lombok.extern.slf4j.Slf4j; |
| | | |
| | | import java.io.BufferedReader; |
| | | import java.io.File; |
| | | import java.io.IOException; |
| | | import java.io.InputStreamReader; |
| | | import java.nio.charset.Charset; |
| | |
| | | @Slf4j(topic = "cmd") |
| | | public class CmdUtils { |
| | | //启动命令1 |
| | | public static void commandStart(String processName, List<String> command, Map<String, String> env) { |
| | | log.debug("启动外部程序:【" + processName+"】"); |
| | | // command.forEach(v -> log.debug(v + " ")); |
| | | public static void commandStart(String workingDir, String processName, List<String> command, Map<String, String> env) { |
| | | log.debug("启动外部程序:【" + processName + "】"); |
| | | //command.forEach(v -> log.debug(v + " ")); |
| | | log.debug(String.join(" ", command)); |
| | | ProcessBuilder builder = new ProcessBuilder(); |
| | | |
| | | // 设置工作目录 |
| | | if (workingDir != null && !workingDir.isEmpty()) { |
| | | builder.directory(new File(workingDir)); |
| | | } |
| | | // 获取子进程的环境变量映射 |
| | | if (env != null) { |
| | | env.entrySet().forEach(entry -> { |
| | |
| | | //启动命令2 |
| | | public static void commandStart(String[] command) { |
| | | //构建启动命令,使用cmd /c start命令来启动可执行程序并显示命令提示符窗口 |
| | | log.debug(String.join(" ", command)); |
| | | ProcessBuilder processBuilder = new ProcessBuilder(command); |
| | | processBuilder.redirectErrorStream(true); // 将错误输出重定向到标准输出 |
| | | Process process = null; |