Example usage for java.lang ProcessBuilder redirectError

List of usage examples for java.lang ProcessBuilder redirectError

Introduction

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

Prototype

public ProcessBuilder redirectError(File file) 

Source Link

Document

Sets this process builder's standard error destination to a file.

Usage

From source file:Main.java

public static void main(String[] args) {

    // create a new list of arguments for our process
    String[] list = { "notepad.exe", "test.txt" };

    // create the process builder
    ProcessBuilder pb = new ProcessBuilder(list);
    try {/*from ww  w .  j  ava  2 s .  c  om*/

        pb = pb.redirectError(new File("c:/"));
        pb.start();
    } catch (IOException ex) {
        ex.printStackTrace();
    }
}

From source file:Main.java

public static void main(String[] args) {

    // create a new list of arguments for our process
    String[] list = { "notepad.exe", "test.txt" };

    // create the process builder
    ProcessBuilder pb = new ProcessBuilder(list);
    try {//from  w ww. j  a  v  a2 s.c o  m

        pb = pb.redirectError(ProcessBuilder.Redirect.from(new File("c:/")));
        pb.start();

    } catch (IOException ex) {
        ex.printStackTrace();
    }
}

From source file:edu.cwru.jpdg.Dotty.java

/**
 * Compiles the graph to dotty./*from w  ww.  j a va 2  s . c  o m*/
 */
public static String dotty(String graph) {

    byte[] bytes = graph.getBytes();

    try {
        ProcessBuilder pb = new ProcessBuilder("dotty");
        pb.redirectError(ProcessBuilder.Redirect.INHERIT);
        Process p = pb.start();
        OutputStream stdin = p.getOutputStream();
        stdin.write(bytes);
        stdin.close();

        String line;
        BufferedReader stdout = new BufferedReader(new InputStreamReader(p.getInputStream()));
        List<String> stdout_lines = new ArrayList<String>();
        line = stdout.readLine();
        while (line != null) {
            stdout_lines.add(line);
            line = stdout.readLine();
        }
        if (p.waitFor() != 0) {
            throw new RuntimeException("javac failed");
        }

        return StringUtils.join(stdout_lines, "\n");
    } catch (InterruptedException e) {
        throw new RuntimeException(e.getMessage());
    } catch (IOException e) {
        throw new RuntimeException(e.getMessage());
    }
}

From source file:com.twitter.bazel.checkstyle.PythonCheckstyle.java

private static void runLinter(List<String> command) throws IOException {
    LOG.finer("checkstyle command: " + command);

    ProcessBuilder processBuilder = new ProcessBuilder(command);
    processBuilder.redirectOutput(ProcessBuilder.Redirect.INHERIT);
    processBuilder.redirectError(ProcessBuilder.Redirect.INHERIT);
    Process pylint = processBuilder.start();

    try {//from   w  w w .  ja v a2  s .c  o  m
        pylint.waitFor();
    } catch (InterruptedException e) {
        throw new RuntimeException("python checkstyle command was interrupted: " + command, e);
    }

    if (pylint.exitValue() == 30) {
        LOG.warning("python checkstyle detected bad styles.");
        // SUPPRESS CHECKSTYLE RegexpSinglelineJava
        System.exit(1);
    }

    if (pylint.exitValue() != 0) {
        throw new RuntimeException("python checkstyle command failed with status " + pylint.exitValue());
    }
}

From source file:com.twitter.bazel.checkstyle.CppCheckstyle.java

private static void runLinter(List<String> command) throws IOException {
    LOG.fine("checkstyle command: " + command);

    ProcessBuilder processBuilder = new ProcessBuilder(command);
    processBuilder.redirectOutput(ProcessBuilder.Redirect.INHERIT);
    processBuilder.redirectError(ProcessBuilder.Redirect.INHERIT);
    Process cpplint = processBuilder.start();

    try {/* w w  w  .  j av a2s.  co m*/
        cpplint.waitFor();
    } catch (InterruptedException e) {
        throw new RuntimeException("cpp checkstyle command was interrupted: " + command, e);
    }

    if (cpplint.exitValue() == 1) {
        LOG.warning("cpp checkstyle detected bad styles.");
        // SUPPRESS CHECKSTYLE RegexpSinglelineJava
        System.exit(1);
    }

    if (cpplint.exitValue() != 0) {
        throw new RuntimeException("cpp checkstyle command failed with status " + cpplint.exitValue());
    }
}

From source file:edu.cwru.jpdg.Dotty.java

public static void graphviz(String name, String graph) {

    byte[] bytes = graph.getBytes();
    try {/*from w w w.j ava  2s  .  c  o m*/
        Path p = Paths.get("./reports/" + name + ".dot");
        Files.write(p, bytes);
    } catch (IOException e) {
        System.out.println(graph);
    }

    try {
        ProcessBuilder pb = new ProcessBuilder("dot", "-Tpng", "-o", "reports/" + name + ".png");
        pb.redirectError(ProcessBuilder.Redirect.INHERIT);
        Process p = pb.start();
        OutputStream stdin = p.getOutputStream();
        stdin.write(bytes);
        stdin.close();

        String line;
        BufferedReader stdout = new BufferedReader(new InputStreamReader(p.getInputStream()));
        List<String> stdout_lines = new ArrayList<String>();
        line = stdout.readLine();
        while (line != null) {
            stdout_lines.add(line);
            line = stdout.readLine();
        }
        if (p.waitFor() != 0) {
            throw new RuntimeException("javac failed");
        }
    } catch (InterruptedException e) {
        throw new RuntimeException(e.getMessage());
    } catch (IOException e) {
        throw new RuntimeException(e.getMessage());
    }
}

From source file:uk.dsxt.voting.tests.TestsLauncher.java

private static void startProcess(String name, String jarPath, String[] params) {
    if (processesByName.containsKey(name)) {
        stopProcess(name);//from  w  ww  . jav  a  2 s  .co m
    }
    try {
        List<String> cmd = new ArrayList<>();
        cmd.add("java");
        cmd.add("-jar");
        cmd.add(jarPath);
        Collections.addAll(cmd, params);

        log.debug("Start process command: {}", StringUtils.join(cmd, " "));

        ProcessBuilder processBuilder = new ProcessBuilder();
        processBuilder.command(cmd);
        processBuilder.redirectError(new File(LOGS_FOLDER, String.format("error_%s.log", name)));
        processBuilder.redirectOutput(new File(LOGS_FOLDER, String.format("output_%s.log", name)));
        Process process = processBuilder.start();
        processesByName.put(name, process);
        log.info("Process {} started", name);
    } catch (Exception e) {
        log.error(String.format("Can't run process %s", name), e);
    }
}

From source file:org.apache.tika.eval.TikaEvalCLITest.java

private static void execute(List<String> incomingArgs, long maxMillis) throws IOException {
    List<String> args = new ArrayList<>();
    String cp = System.getProperty("java.class.path");
    args.add("java");
    args.add("-cp");
    args.add(cp);/*from w  ww . j a  v  a  2 s  . c  o  m*/
    args.add("org.apache.tika.eval.TikaEvalCLI");
    args.addAll(incomingArgs);

    ProcessBuilder pb = new ProcessBuilder(args);
    pb.redirectOutput(ProcessBuilder.Redirect.INHERIT);
    pb.redirectError(ProcessBuilder.Redirect.INHERIT);
    Process process = pb.start();
    long started = new Date().getTime();
    long elapsed = new Date().getTime() - started;
    int exitValue = Integer.MIN_VALUE;
    while (elapsed < maxMillis && exitValue == Integer.MIN_VALUE) {
        try {
            exitValue = process.exitValue();
        } catch (IllegalThreadStateException e) {

        }
        elapsed = new Date().getTime() - started;
    }
    if (exitValue == Integer.MIN_VALUE) {
        process.destroy();
        throw new RuntimeException(
                "Process never exited within the allowed amount of time.\n" + "I needed to destroy it");
    }
}

From source file:org.dkpro.tc.ml.svmhmm.task.SVMHMMTestTask.java

/**
 * Executes the command (runs a new process outside the JVM and waits for its completion)
 *
 * @param command//www.j a va  2s.c  o m
 *            command as list of Strings
 */
private static void runCommand(List<String> command) throws Exception {
    log.info(StringUtils.join(command, " "));

    if (PRINT_STD_OUT) {
        Process process = new ProcessBuilder().inheritIO().command(command).start();
        process.waitFor();
    } else {
        // create temp files for capturing output instead of printing to
        // stdout/stderr
        File tmpOutLog = File.createTempFile("tmp.out.", ".log");

        ProcessBuilder processBuilder = new ProcessBuilder(command);

        processBuilder.redirectError(tmpOutLog);
        processBuilder.redirectOutput(tmpOutLog);

        // run the process
        Process process = processBuilder.start();
        process.waitFor();

        // re-read the output and debug it
        BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(tmpOutLog)));
        String line;
        while ((line = br.readLine()) != null) {
            log.debug(line);
        }
        IOUtils.closeQuietly(br);

        // delete files
        FileUtils.deleteQuietly(tmpOutLog);
    }
}

From source file:de.tudarmstadt.ukp.dkpro.tc.svmhmm.task.SVMHMMTestTask.java

/**
 * Executes the command (runs a new process outside the JVM and waits for its completion)
 *
 * @param command command as list of Strings
 *///from ww  w .  j  av  a  2s.c om
private static void runCommand(List<String> command) throws Exception {
    log.info(StringUtils.join(command, " "));

    if (PRINT_STD_OUT) {
        Process process = new ProcessBuilder().inheritIO().command(command).start();
        process.waitFor();
    } else {
        // create temp files for capturing output instead of printing to stdout/stderr
        File tmpOutLog = File.createTempFile("tmp.out.", ".log");

        ProcessBuilder processBuilder = new ProcessBuilder(command);

        processBuilder.redirectError(tmpOutLog);
        processBuilder.redirectOutput(tmpOutLog);

        // run the process
        Process process = processBuilder.start();
        process.waitFor();

        // re-read the output and debug it
        BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(tmpOutLog)));
        String line;
        while ((line = br.readLine()) != null) {
            log.debug(line);
        }
        IOUtils.closeQuietly(br);

        // delete files
        FileUtils.deleteQuietly(tmpOutLog);
    }
}