zoukankan      html  css  js  c++  java
  • java execShell

    public static List<String> execShell(String workspace, String... shellStrings) {
            List<String> resultList = new ArrayList<>();
            StringBuilder msg = new StringBuilder();
            File dir = null;
            if (workspace != null) {
                msg.append(String.format("execShell workspace=%s
    ", workspace));
                dir = new File(workspace);
            }
            for (String shellString : shellStrings) {
                msg.append(String.format("pre-execShell %s", shellString));
                StringBuilder result = new StringBuilder();
                try {
                    Process process;
                    if (IS_OS_WINDOWS) {
                        process = Runtime.getRuntime().exec(shellString, null, dir);
                    } else {
                        process = Runtime.getRuntime().exec(new String[]{"/bin/sh", "-c", shellString}, null, dir);
                    }
                    try (LineNumberReader input = new LineNumberReader(
                            new InputStreamReader(process.getInputStream(), StandardCharsets.UTF_8))) {
                        String line;
                        while ((line = input.readLine()) != null) {
                            result.append(line).append("
    ");
                        }
                    }
                } catch (Exception e) {
                    log.error("execShell", e);
                    result.append("ERROR: " + e.getMessage()).append("
    ");
                }
                msg.append(String.format("
    post-execShell %s
    
    ", result));
                if (result.getLength() == 0) {
                    result.append("NULL");
                }
                resultList.add(result.toString());
            }
            log.info(msg.toString());
            return resultList;
        }
  • 相关阅读:
    vue 中使用阿里iconfont彩色图标
    团队作业九
    团队作业八
    团队作业七
    第二篇
    第三篇
    第一篇
    beta冲刺计划安排
    团队作业六
    团队作业五
  • 原文地址:https://www.cnblogs.com/Googler/p/14224835.html
Copyright © 2011-2022 走看看