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;
        }
  • 相关阅读:
    Network(树形dp)洛谷2899
    2590 树的统计
    LCT 最小生成树
    几种贪心小结
    snmp
    div页面跳转
    2017.11.2总结,回顾及成果
    2017.11.1知识总结及回顾
    check,form,单选框与复选框总结
    HTML空格字符
  • 原文地址:https://www.cnblogs.com/Googler/p/14224835.html
Copyright © 2011-2022 走看看