package com.as.common;
import java.io.DataOutputStream;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;


public class ShellUtils {

    private ShellUtils() {
        throw new AssertionError();
    }

    public static CommandResult execCommand(String command) {
        return execCommand(new String[]{command}, ".", true);
    }

    public static int execCommand(String[] commands, String path) {
        return execCommand(commands, path, false).result;
    }

    public static CommandResult execCommand(String[] commands, String path, boolean isNeedResultMsg) {
        int result = -1;
        if (commands == null || commands.length == 0) {
            return new CommandResult(result, null, null);
        }

        Process process = null;
        InputStream successResult = null;
        InputStream errorResult = null;
        StringBuilder successMsg = null;
        StringBuilder errorMsg = null;

        DataOutputStream os = null;
        try {
//            process = Runtime.getRuntime().exec(isRoot ? COMMAND_SU : COMMAND_SH);
            process = Runtime.getRuntime().exec(commands, null, new File(path));
//            os = new DataOutputStream(process.getOutputStream());
//            for (String command : commands) {
//                if (command == null) {
//                    continue;
//                }
//
//                 donnot use os.writeBytes(commmand), avoid chinese charset error
//                os.write(command.getBytes());
//                os.writeBytes(COMMAND_LINE_END);
//                os.flush();
//            }
//            os.writeBytes(COMMAND_EXIT);
//            os.flush();

            result = process.waitFor();
            // get command result
            if (isNeedResultMsg) {
                successMsg = new StringBuilder();
                errorMsg = new StringBuilder();
                successResult = process.getInputStream();
                errorResult = process.getErrorStream();

                byte[] buf = new byte[1024];
                for (int n; (n = successResult.read(buf)) != -1; ) {
                    successMsg.append(new String(buf, 0, n));
                }

                for (int n; (n = errorResult.read(buf)) != -1; ) {
                    errorMsg.append(new String(buf, 0, n));
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                if (os != null) {
                    os.close();
                }
                if (successResult != null) {
                    successResult.close();
                }
                if (errorResult != null) {
                    errorResult.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }

            if (process != null) {
                process.destroy();
            }
        }
        return new CommandResult(result, successMsg == null ? null : successMsg.toString(), errorMsg == null ? null
                : errorMsg.toString());
    }

    /**
     * result of command
     * <ul>
     * <li>{@link CommandResult#result} means result of command, 0 means normal, else means error, same to excute in
     * linux shell</li>
     * <li>{@link CommandResult#successMsg} means success message of command result</li>
     * <li>{@link CommandResult#errorMsg} means error message of command result</li>
     * </ul>
     *
     * @author <a href="http://www.trinea.cn" target="_blank">Trinea</a> 2013-5-16
     */
    @SuppressWarnings("unused")
    public static class CommandResult {

        /**
         * result of command
         **/
        public int result;
        /**
         * success message of command result
         **/
        public String successMsg;
        /**
         * error message of command result
         **/
        public String errorMsg;

        public CommandResult(int result) {
            this.result = result;
        }

        public CommandResult(int result, String successMsg, String errorMsg) {
            this.result = result;
            this.successMsg = successMsg;
            this.errorMsg = errorMsg;
        }
    }
}
最近下载更多
whfuai  LV14 2022年7月14日
丶右转  LV11 2021年12月3日
byj1987  LV18 2021年7月8日
yl123456235346254  LV1 2021年6月15日
koumeiyuu  LV8 2021年3月19日
zeng2813  LV2 2020年10月30日
czy725  LV1 2020年8月5日
渣渣辉  LV11 2020年5月26日
zhengcl  LV3 2020年4月7日
syh12355  LV5 2019年11月25日
最近浏览更多
nurmamat001  LV2 2022年11月1日
whfuai  LV14 2022年7月14日
林间听风  LV10 2022年6月24日
xiao妮子  LV5 2022年6月4日
cc900118  LV17 2022年4月25日
crosa_Don  LV18 2022年3月31日
丶右转  LV11 2021年12月3日
byj1987  LV18 2021年7月8日
yl123456235346254  LV1 2021年6月15日
212828939  LV16 2021年5月12日
顶部 客服 微信二维码 底部
>扫描二维码关注最代码为好友扫描二维码关注最代码为好友