zoukankan      html  css  js  c++  java
  • java 调用命令行工具类

    runCmdOnDir是指定目录执行的函数
    package cn.com.ruijie.rgonc.grpc.impl.utils;
    
    import java.io.*;
    
    public class CommandUtil {
        public static void runCMD(String[] CMD) {
            java.lang.Process process = null;
            try {
                process = Runtime.getRuntime().exec(CMD);
                ByteArrayOutputStream resultOutStream = new ByteArrayOutputStream();
                InputStream errorInStream = new BufferedInputStream(process.getErrorStream());
                InputStream processInStream = new BufferedInputStream(process.getInputStream());
                int num = 0;
                byte[] bs = new byte[1024];
                while ((num = errorInStream.read(bs)) != -1) {
                    resultOutStream.write(bs, 0, num);
                }
                while ((num = processInStream.read(bs)) != -1) {
                    resultOutStream.write(bs, 0, num);
                }
                String result = new String(resultOutStream.toByteArray(), "gbk");
                System.out.println(result);
                errorInStream.close();
                processInStream.close();
                resultOutStream.close();
            } catch (IOException e) {
                e.printStackTrace();
            } finally {
                if (process != null) process.destroy();
            }
        }
    
        public static void runCmdOnDir(String[] CMD, String path) {
            java.lang.Process process = null;
            try {
                process = Runtime.getRuntime().exec(CMD, null, new File(path));
                ByteArrayOutputStream resultOutStream = new ByteArrayOutputStream();
                InputStream errorInStream = new BufferedInputStream(process.getErrorStream());
                InputStream processInStream = new BufferedInputStream(process.getInputStream());
                int num = 0;
                byte[] bs = new byte[1024];
                while ((num = errorInStream.read(bs)) != -1) {
                    resultOutStream.write(bs, 0, num);
                }
                while ((num = processInStream.read(bs)) != -1) {
                    resultOutStream.write(bs, 0, num);
                }
                String result = new String(resultOutStream.toByteArray(), "gbk");
                System.out.println(result);
                errorInStream.close();
                processInStream.close();
                resultOutStream.close();
            } catch (IOException e) {
                e.printStackTrace();
            } finally {
                if (process != null) process.destroy();
            }
        }
    }
  • 相关阅读:
    navicat preminm 12安裝及破解
    fiddler 延长某个特定资源或接口的返回时长
    linux 命令更新
    fiddler 简单的接口性能测试replay
    fiddler 设置断点修改请求,响应数据及模拟响应
    fiddler 模拟弱网测试
    ubuntu 谷歌浏览器打开时需要输入密码来解锁密码环
    ubuntu google chrome 忽略证书错误 -- 解决自签名证书不支持的问题
    ubuntu google chrome 全屏显示命令
    开机出错提示 cpu fan speed error detected
  • 原文地址:https://www.cnblogs.com/chenmz1995/p/13430830.html
Copyright © 2011-2022 走看看