zoukankan      html  css  js  c++  java
  • JAVA 执行系统命令

    import java.io.BufferedReader;
    import java.io.IOException;
    import java.io.InputStream;
    import java.io.InputStreamReader;
    
    
    public class TestJavaCommand {
        /**
         * 执行命令
         */
        public static void executeCommand() {
            Runtime runtime = Runtime.getRuntime();
            try {
                String[] command = {"java", "-version"};
                Process process = runtime.exec(command);
                // 标准输入流(必须写在 waitFor 之前)
                String inStr = consumeInputStream(process.getInputStream());
                // 标准错误流(必须写在 waitFor 之前)
                String errStr = consumeInputStream(process.getErrorStream()); //若有错误信息则输出
                int proc = process.waitFor();
                if (proc == 0) {
                } else {
                    System.out.println("执行失败");
                }
            } catch (IOException | InterruptedException e) {
                e.printStackTrace();
            }
        }
    
        /**
         * @param is
         * @return
         * @throws IOException
         */
        private static String consumeInputStream(InputStream is) throws IOException {
            BufferedReader br = new BufferedReader(new InputStreamReader(is, "GBK"));
            String s;
            StringBuilder sb = new StringBuilder();
            while ((s = br.readLine()) != null) {
                System.out.println(s);
                sb.append(s);
            }
            return sb.toString();
        }
    }
  • 相关阅读:
    汉语-词语:注重
    汉语-词语:解释
    汉语-词语:接受
    汉语-词语:专注
    汉语-词语:构想
    生物-植物-果树:枣树
    汉语-词语:维度
    汉语-词语:真传
    XML基础知识学习
    Java Swing界面编程(25)---事件处理:鼠标事件及监听处理
  • 原文地址:https://www.cnblogs.com/guliang/p/15635016.html
Copyright © 2011-2022 走看看