Example usage for java.lang ProcessBuilder redirectErrorStream

List of usage examples for java.lang ProcessBuilder redirectErrorStream

Introduction

In this page you can find the example usage for java.lang ProcessBuilder redirectErrorStream.

Prototype

boolean redirectErrorStream

To view the source code for java.lang ProcessBuilder redirectErrorStream.

Click Source Link

Usage

From source file:net.sourceforge.pmd.it.CpdExecutor.java

private static ExecutionResult runCpdUnix(Path tempDir, String... arguments) throws Exception {
    String cmd = tempDir.resolve(PMD_BIN_PREFIX + PMDVersion.VERSION + "/bin/run.sh").toAbsolutePath()
            .toString();/*  w  w w .j a v a  2  s. c o  m*/
    ProcessBuilder pb = new ProcessBuilder(cmd, "cpd");
    pb.command().addAll(Arrays.asList(arguments));
    pb.redirectErrorStream(true);
    Process process = pb.start();
    String output = IOUtils.toString(process.getInputStream(), StandardCharsets.UTF_8);

    int result = process.waitFor();
    return new ExecutionResult(result, output);
}

From source file:edu.uci.ics.asterix.installer.test.AsterixClusterLifeCycleIT.java

private static Process invoke(String... args) throws Exception {
    ProcessBuilder pb = new ProcessBuilder(args);
    pb.redirectErrorStream(true);
    Process p = pb.start();/*from   w w w . ja v  a2 s .  c  o  m*/
    return p;
}

From source file:end2endtests.runner.ProcessRunner.java

private static Process startProcess(File workingDir, List<String> command) throws IOException {
    ProcessBuilder builder = new ProcessBuilder();
    builder.directory(workingDir);//from w w  w .ja v a 2  s  .  c  o m
    builder.redirectErrorStream(false);
    builder.command(command);
    return builder.start();
}

From source file:net.sourceforge.pmd.it.PMDExecutor.java

private static ExecutionResult runPMDWindows(Path tempDir, String... arguments) throws Exception {
    String cmd = tempDir.resolve(PMD_BIN_PREFIX + PMDVersion.VERSION + "/bin/pmd.bat").toAbsolutePath()
            .toString();//from   w  ww  .j a v  a 2 s  .  co m
    ProcessBuilder pb = new ProcessBuilder(cmd);
    pb.command().addAll(Arrays.asList(arguments));
    pb.redirectErrorStream(true);
    Process process = pb.start();
    String output = IOUtils.toString(process.getInputStream(), StandardCharsets.UTF_8);

    int result = process.waitFor();
    return new ExecutionResult(result, output);
}

From source file:mitm.common.sms.transport.gnokii.Gnokii.java

public static String identify() throws GnokiiException {
    String[] cmd = new String[] { GNOKII_BIN, "--identify" };

    try {/*from w w  w. jav  a 2s  . c o  m*/
        ProcessBuilder processBuilder = new ProcessBuilder(cmd);
        processBuilder.redirectErrorStream(true);

        Process process = processBuilder.start();

        String output = IOUtils.toString(process.getInputStream());

        int exitValue;

        try {
            exitValue = process.waitFor();
        } catch (InterruptedException e) {
            throw new GnokiiException("Identify error. Output: " + output);
        }

        if (exitValue != 0) {
            throw new GnokiiException("Identify error. Output: " + output);
        }

        return output;
    } catch (IOException e) {
        throw new GnokiiException(e);
    }
}

From source file:net.sourceforge.pmd.it.PMDExecutor.java

private static ExecutionResult runPMDUnix(Path tempDir, String... arguments) throws Exception {
    String cmd = tempDir.resolve(PMD_BIN_PREFIX + PMDVersion.VERSION + "/bin/run.sh").toAbsolutePath()
            .toString();/*w  w w. j  a  v a  2  s.  co  m*/
    ProcessBuilder pb = new ProcessBuilder(cmd, "pmd");
    pb.command().addAll(Arrays.asList(arguments));
    pb.redirectErrorStream(true);
    Process process = pb.start();
    String output = IOUtils.toString(process.getInputStream(), StandardCharsets.UTF_8);

    int result = process.waitFor();
    return new ExecutionResult(result, output);
}

From source file:org.jsweet.util.ProcessUtil.java

public static void runCmd(File directory, Consumer<String> stdoutConsumer, String... cmd) {
    System.out.println("run command: " + StringUtils.join(cmd, " "));

    String[] args;/*from w  w  w.  ja v a  2  s  .  co  m*/
    if (System.getProperty("os.name").startsWith("Windows")) {
        args = new String[] { "cmd", "/c" };
    } else {
        args = new String[0];
    }
    args = ArrayUtils.addAll(args, cmd);

    System.out.println("run command: '" + StringUtils.join(args, " ") + "' in directory " + directory);
    // logger.fine("run command: " + StringUtils.join(args, " "));
    int code = -1;
    try {

        ProcessBuilder processBuilder = new ProcessBuilder(args);
        processBuilder.redirectErrorStream(true);
        if (directory != null) {
            processBuilder.directory(directory);
        }
        if (!StringUtils.isBlank(EXTRA_PATH)) {
            processBuilder.environment().put("PATH",
                    processBuilder.environment().get("PATH") + File.pathSeparator + EXTRA_PATH);
        }

        Process process = processBuilder.start();

        try (BufferedReader in = new BufferedReader(new InputStreamReader(process.getInputStream()))) {
            String line;
            while ((line = in.readLine()) != null) {
                if (stdoutConsumer != null) {
                    stdoutConsumer.accept(line);
                } else {
                    logger.info("OUT:" + line);
                }
            }
        }

        code = process.waitFor();
        if (code != 0) {
            throw new RuntimeException(
                    "error while exectuting: " + StringUtils.join(args, " ") + ", error code: " + code);
        }
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}

From source file:org.apache.jackrabbit.webdav.simple.LitmusTest.java

private static boolean isLitmusAvailable(String litmus) {
    try {//w  ww  .j  a va2  s .c  o m
        ProcessBuilder builder = new ProcessBuilder(litmus, "--version");
        builder.redirectErrorStream();
        Process process = builder.start();

        ByteArrayOutputStream buffer = new ByteArrayOutputStream();
        IOUtils.copy(process.getInputStream(), buffer);
        int rv = process.waitFor();
        log.info("litmus version: {}", buffer.toString("US-ASCII").trim());

        return rv == 0;
    } catch (Exception e) {
        log.warn("litmus is not available: " + litmus, e);
        return false;
    }
}

From source file:Main.java

private static void killprocessNormal(String proc, int killMethod) {
    try {/*ww  w. j  a v a  2 s.com*/
        ArrayList<String> pid_list = new ArrayList<String>();

        ProcessBuilder execBuilder = null;

        execBuilder = new ProcessBuilder("sh", "-c", "ps |grep " + proc);

        execBuilder.redirectErrorStream(true);

        Process exec = null;
        exec = execBuilder.start();
        InputStream is = exec.getInputStream();
        BufferedReader reader = new BufferedReader(new InputStreamReader(is));

        String line = "";
        while ((line = reader.readLine()) != null) {
            String regEx = "\\s[0-9][0-9]*\\s";
            Pattern pat = Pattern.compile(regEx);
            Matcher mat = pat.matcher(line);
            if (mat.find()) {
                String temp = mat.group();
                temp = temp.replaceAll("\\s", "");
                pid_list.add(temp);
            }
        }

        for (int i = 0; i < pid_list.size(); i++) {
            execBuilder = new ProcessBuilder("su", "-c", "kill", "-" + killMethod, pid_list.get(i));
            exec = null;
            exec = execBuilder.start();

            execBuilder = new ProcessBuilder("su", "-c", "kill" + " -" + killMethod + " " + pid_list.get(i));
            exec = null;
            exec = execBuilder.start();
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
}

From source file:ml.shifu.shifu.executor.ProcessManager.java

public static int runShellProcess(String currentDir, String[] args) throws IOException {
    ProcessBuilder processBuilder = new ProcessBuilder(args);
    processBuilder.directory(new File(currentDir));
    processBuilder.redirectErrorStream(true);
    Process process = processBuilder.start();

    LogThread logThread = new LogThread(process, process.getInputStream(), currentDir);
    logThread.start();//  w ww  . java  2  s .  com

    try {
        process.waitFor();
    } catch (InterruptedException e) {
        process.destroy();
    } finally {
        logThread.setToQuit(true);
    }

    LOG.info("Under {} directory, finish run `{}`", currentDir, args);
    return process.exitValue();
}