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 @ 2023-04-19 18:49  岑惜  阅读(130)  评论(0)    收藏  举报