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;
        }
  • 相关阅读:
    LightOJ 1030 Discovering Gold(期望)
    CodeForces 567B Berland National Library
    HDU
    HDU
    (模拟、进制转换)
    HDU
    HDU
    CodeForces 429 B B. Working out
    CodeForces 546 D. Soldier and Number Game(素数有关)
    2016中国大学生程序设计竞赛
  • 原文地址:https://www.cnblogs.com/Googler/p/14224835.html
Copyright © 2011-2022 走看看