java - 调window系统指令的三种方式

方法1

        try {
            Process process = Runtime.getRuntime().exec(command);
            int resultCode = process.waitFor();
            System.out.println(resultCode);
        } catch (Exception e) {
            e.printStackTrace();
            msg = ExceptionUtils.getErrorStackTrace(e);
        }

方法2

复制代码
  Process process = null;
        try {
            ProcessBuilder pb = new ProcessBuilder();
            //指令太长会导致无法找到文件,被视为一个完整的指令,应该每个空格都是为一个指令
            List<String> ls = new ArrayList<>(Arrays.asList(command.split(" ")));
            pb.command(ls);
            //processBuilder支持将inputStream与ErrorStream合并为一个Stream,即所有的输出信息都合并到inputStream中,这样做可以减少一个线程
            pb.redirectErrorStream(true);
            process = pb.start();
            //由于process机制原因会导致死锁,所以需要在waitfor方法之前,创建线程用于处理inputstream中缓冲区的数据,这也是为什么要合并inputstream和errorstream的原因,在这里可以少创建一个线程
            readInputStream(process.getInputStream());
            //返回0则表示输出正常
            int resultCode = process.waitFor();
        } catch (Exception e) {
            e.printStackTrace();
            msg = ExceptionUtils.getErrorStackTrace(e);
        } finally {
            try {
                if (null != process) {
                    process.getErrorStream().close();
                    process.getInputStream().close();
                    process.getOutputStream().close();
                }
            } catch (Exception ignored) {
            }
        }
复制代码
复制代码

private static ExecutorService executor;

static {
//创建线程池
executor = Executors.newFixedThreadPool(10);
}


//
创建线程处理输出流 private static void readInputStream(InputStream in) { executor.execute(new Runnable() { @Override public void run() { InputStreamReader reader = null; try { reader = new InputStreamReader(in); LineNumberReader line = new LineNumberReader(reader); String str = null; while ((str = line.readLine()) != null) { System.out.println(str); } } catch (Exception e) { e.printStackTrace(); } finally { try { if (reader != null) { reader.close(); } } catch (IOException e) { e.printStackTrace(); } } } }); }
复制代码

方法3

复制代码
 public static boolean runCMD(String cmd) throws IOException, InterruptedException {
        Process p = Runtime.getRuntime().exec(cmd);
        BufferedReader br = null;
        try {
            br = new BufferedReader(new InputStreamReader(p.getInputStream()));
            String readLine = br.readLine();
            StringBuilder builder = new StringBuilder();
            while (readLine != null) {
                readLine = br.readLine();
                builder.append(readLine);
            }
            p.waitFor();
            int i = p.exitValue();
            return i == 0;
        } catch (Exception e) {
            e.printStackTrace();
            throw e;
        } finally {
            if (br != null) {
                br.close();
            }
        }
    }
复制代码
 try {
            runCMD(command);
        } catch (IOException | InterruptedException e) {
            e.printStackTrace();
        }

 

posted @   岑惜  阅读(114)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· ollama系列01:轻松3步本地部署deepseek,普通电脑可用
· 按钮权限的设计及实现
· 25岁的心里话
点击右上角即可分享
微信分享提示