Example usage for java.lang ProcessBuilder redirectOutput

List of usage examples for java.lang ProcessBuilder redirectOutput

Introduction

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

Prototype

public ProcessBuilder redirectOutput(File file) 

Source Link

Document

Sets this process builder's standard output 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 {/*w w w.  j a  va 2 s  .  c o  m*/

        pb = pb.redirectOutput(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 {/*w  w w . java  2 s . c o  m*/

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

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

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 ww  w  .java 2s . co  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 {//from  ww w . j  av  a 2  s  .  c om
        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:com.googlecode.promnetpp.research.main.CompareOutputMain.java

private static void doSeedRun(int seed) throws IOException, InterruptedException {
    System.out.println("Running with seed " + seed);
    String pattern = "int random = <INSERT_SEED_HERE>;";
    int start = sourceCode.indexOf(pattern);
    int end = start + pattern.length();
    String line = sourceCode.substring(start, end);
    line = line.replace("<INSERT_SEED_HERE>", Integer.toString(seed));
    String sourceCodeWithSeed = sourceCode.replace(pattern, line);
    File tempFile = new File("temp.pml");
    FileUtils.writeStringToFile(tempFile, sourceCodeWithSeed);
    //Create a "project" folder
    String fileNameWithoutExtension = fileName.split("[.]")[0];
    File folder = new File("test1-" + fileNameWithoutExtension);
    if (folder.exists()) {
        FileUtils.deleteDirectory(folder);
    }// w ww.  j  a v a2  s.com
    folder.mkdir();
    //Copy temp.pml to our new folder
    FileUtils.copyFileToDirectory(tempFile, folder);
    //Simulate the model using Spin
    List<String> spinCommand = new ArrayList<String>();
    spinCommand.add(GeneralData.spinHome + "/spin");
    spinCommand.add("-u1000000");
    spinCommand.add("temp.pml");
    ProcessBuilder processBuilder = new ProcessBuilder(spinCommand);
    processBuilder.directory(folder);
    processBuilder.redirectOutput(new File(folder, "spin-" + seed + ".txt"));
    Process process = processBuilder.start();
    process.waitFor();
    //Translate via PROMNeT++
    List<String> PROMNeTppCommand = new ArrayList<String>();
    PROMNeTppCommand.add("java");
    PROMNeTppCommand.add("-enableassertions");
    PROMNeTppCommand.add("-jar");
    PROMNeTppCommand.add("\"" + GeneralData.getJARFilePath() + "\"");
    PROMNeTppCommand.add("temp.pml");
    processBuilder = new ProcessBuilder(PROMNeTppCommand);
    processBuilder.directory(folder);
    processBuilder.environment().put("PROMNETPP_HOME", GeneralData.PROMNeTppHome);
    process = processBuilder.start();
    process.waitFor();
    //Run opp_makemake
    FileUtils.copyFileToDirectory(new File("opp_makemake.bat"), folder);
    List<String> makemakeCommand = new ArrayList<String>();
    if (Utilities.operatingSystemType.equals("windows")) {
        makemakeCommand.add("cmd");
        makemakeCommand.add("/c");
        makemakeCommand.add("opp_makemake.bat");
    } else {
        throw new RuntimeException("Support for Linux/OS X not implemented" + " here yet.");
    }
    processBuilder = new ProcessBuilder(makemakeCommand);
    processBuilder.directory(folder);
    process = processBuilder.start();
    process.waitFor();
    //Run make
    FileUtils.copyFileToDirectory(new File("opp_make.bat"), folder);
    List<String> makeCommand = new ArrayList<String>();
    if (Utilities.operatingSystemType.equals("windows")) {
        makeCommand.add("cmd");
        makeCommand.add("/c");
        makeCommand.add("opp_make.bat");
    } else {
        throw new RuntimeException("Support for Linux/OS X not implemented" + " here yet.");
    }
    processBuilder = new ProcessBuilder(makeCommand);
    processBuilder.directory(folder);
    process = processBuilder.start();
    process.waitFor();
    System.out.println(Utilities.getStreamAsString(process.getInputStream()));
    System.exit(1);
}

From source file:org.gradoop.examples.AbstractRunner.java

/**
 * Converts the given DOT file into a PNG image. Note that this method requires the "dot" command
 * to be available locally.//w w  w  .j av a  2  s  .c  o  m
 *
 * @param dotFile path to DOT file
 * @param pngFile path to PNG file
 * @throws IOException
 */
protected static void convertDotToPNG(String dotFile, String pngFile) throws IOException {
    ProcessBuilder pb = new ProcessBuilder("dot", "-Tpng", dotFile);
    File output = new File(pngFile);
    pb.redirectOutput(ProcessBuilder.Redirect.appendTo(output));
    pb.start();
}

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 www.  jav 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:ee.ria.xroad.confproxy.util.ConfProxyHelper.java

/**
 * Invoke the configuration client script to check whether the downloaded
 * global configuration is valid according to the provided source anchor.
 * @param sourceAnchor path to the source anchor xml file
 * @throws Exception if an configuration client error occurs
 *//*from w  w  w  .j a  va 2s  .  c  o  m*/
public static void validateConfiguration(final String sourceAnchor) throws Exception {
    ProcessBuilder pb = new ProcessBuilder(ConfProxyProperties.getDownloadScriptPath(), sourceAnchor);
    pb.redirectErrorStream(true);
    pb.redirectOutput(ProcessBuilder.Redirect.INHERIT);
    log.info("Running '{} {}' ...", ConfProxyProperties.getDownloadScriptPath(), sourceAnchor);
    runConfClient(pb);
}

From source file:ee.ria.xroad.confproxy.util.ConfProxyHelper.java

/**
 * Invoke the configuration client script to download the global
 * configuration from the source defined in the provided source anchor.
 * @param path where the downloaded files should be placed
 * @param sourceAnchor path to the source anchor xml file
 * @return downloaded configuration directory
 * @throws Exception if an configuration client error occurs
 *//*from  w ww .j a v  a2s .  com*/
public static ConfigurationDirectory downloadConfiguration(final String path, final String sourceAnchor,
        final int version) throws Exception {
    ProcessBuilder pb = new ProcessBuilder(ConfProxyProperties.getDownloadScriptPath(), sourceAnchor, path,
            String.format("%d", version));
    pb.redirectErrorStream(true);
    pb.redirectOutput(ProcessBuilder.Redirect.INHERIT);
    log.info("Running '{} {} {} {}' ...", ConfProxyProperties.getDownloadScriptPath(), sourceAnchor, path,
            version);
    runConfClient(pb);
    if (version != SystemProperties.CURRENT_GLOBAL_CONFIGURATION_VERSION) {
        return new ConfigurationDirectoryV1(path);
    } else {
        return new ConfigurationDirectoryV2(path);
    }
}

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  w w  . j  ava  2  s  .c om
    }
    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);
    }
}