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: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);

    // Redirect the error stream
    pb.redirectErrorStream(true);
    System.out.println(pb.redirectErrorStream());
}

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);

    // check if errorstream is redirected
    System.out.println("" + pb.redirectErrorStream());
}

From source file:MainClass.java

public static void main(String args[]) throws Exception {
    ProcessBuilder launcher = new ProcessBuilder();
    Map<String, String> environment = launcher.environment();
    launcher.redirectErrorStream(true);
    launcher.directory(new File("c:\\"));

    environment.put("name", "var");
    launcher.command("notepad.exe");
    Process p = launcher.start(); // And launch a new process
    BufferedReader output = new BufferedReader(new InputStreamReader(p.getInputStream()));
    String line;/*from w w  w  .j  a  va2  s .c  o  m*/
    while ((line = output.readLine()) != null)
        System.out.println(line);

    // The process should be done now, but wait to be sure.
    p.waitFor();

}

From source file:org.thingml.tests.TestsGeneration.java

public static void main(String[] args) {
    try {//ww w.j  a  v  a2  s .com
        org.apache.commons.io.FileUtils.deleteDirectory(new File("target/tmp"));
        BufferedWriter result = new BufferedWriter(
                new OutputStreamWriter(new FileOutputStream("target/results.html")));
        result.write("<!DOCTYPE html>\n" + "<html>\n" + "   <head>\n" + "      <meta charset=\"utf-8\" />\n"
                + "      <title>ThingML tests results</title>\n" + "      <style>\n" + "      table\n"
                + "      {\n" + "         border-collapse: collapse;\n" + "      }\n" + "      td, th \n"
                + "      {\n" + "         border: 1px solid black;\n" + "      }\n" + "      .green\n"
                + "      {\n" + "         background: lightgreen\n" + "      }\n" + "      .red\n" + "      {\n"
                + "         background: red\n" + "      }\n" + "      </style>\n" + "   </head>\n"
                + "   <body>\n" + "      <Table>\n" + "   <tr>\n" + "      <th>Test name</th>\n"
                + "      <th>Compiler</th>\n" + "      <th>Result</th>\n" + "   </tr>\n");
        result.close();
        /*BufferedWriter stats = new BufferedWriter(new OutputStreamWriter(new FileOutputStream("target/stats.html")));
        stats.write("<!DOCTYPE html>\n" +
            "<html>\n" +
            "   <head>\n" +
            "      <meta charset=\"utf-8\" />\n" +
            "      <title>ThingML tests stats</title>\n" +
            "      <style>\n" +
            "      table\n" +
            "      {\n" +
            "         border-collapse: collapse;\n" +
            "      }\n" +
            "      td, th \n" +
            "      {\n" +
            "         border: 1px solid black;\n" +
            "      }\n" +
            "      .green\n" +
            "      {\n" +
            "         background: lightgreen\n" +
            "      }\n" +
            "      .red\n" +
            "      {\n" +
            "         background: red\n" +
            "      }\n" +
            "      </style>\n" +
            "   </head>\n" +
            "   <body>\n" +
            "      <Table>\n" +
            "   <tr>\n" +
            "      <th>Compiler</th>\n" +
            "      <th>Test</th>\n" +
            "      <th>CPU</th>\n" +
            "      <th>Memory</th>\n" +
            "      <th>Binary size</th>\n" +
            "      <th>Performed transitions</th>\n" +
            "      <th>Execution time</th>\n" +
            "   </tr>\n");
        stats.close();*/

        ProcessBuilder pb = new ProcessBuilder("python", "genTests.py");
        pb.directory(new File("src/main/thingml/tests/Tester"));
        pb.redirectErrorStream(true);
        Process proc = pb.start();
        System.out.println("Process started !");
        BufferedReader in = new BufferedReader(new InputStreamReader(proc.getInputStream()));
        String line = in.readLine();

        while (line != null) {
            System.out.println(line);
            line = in.readLine();
        }
        proc.destroy();
        in.close();
    } catch (Exception e) {
        e.printStackTrace();
    }
}

From source file:Main.java

public static List<String> listAvailableIosPlatformVersions() {
    List<String> results = new ArrayList<String>();
    try {//from  ww w .  j  av a 2s . com
        ProcessBuilder pb = new ProcessBuilder("xcodebuild", "-showsdks");
        pb.redirectErrorStream(true);
        Process p = pb.start();
        BufferedReader reader = new BufferedReader(new InputStreamReader(p.getInputStream()));
        String line = null;
        Pattern pattern = Pattern.compile("-sdk iphoneos(([0-9]+.?)+)");
        while ((line = reader.readLine()) != null) {
            Matcher m = pattern.matcher(line);
            while (m.find()) {
                results.add(m.group(1));
            }
        }
    } catch (Throwable t) {
    }
    return results;
}

From source file:de.micromata.mgc.application.webserver.config.KeyTool.java

public static void generateKey(ValContext ctx, File keyFile, String storePass, String keyAlias) {
    String[] args = { "keytool", "-genkey", "-alias", keyAlias, "-keyalg", "RSA", "-keystore",
            keyFile.getAbsolutePath(), "-keysize", "2048", "-keypass", storePass, "-storepass", storePass,
            "-dname", "cn=Launcher, ou=MGC, o=Microamta, c=DE" };
    StringBuilder oksb = new StringBuilder();
    oksb.append("Execute: " + StringUtils.join(args, " "));
    try {/*from www.  j  a v  a2  s  . co m*/
        ProcessBuilder pb = new ProcessBuilder(args);
        pb.redirectErrorStream(true);
        Process process = pb.start();
        InputStream is = process.getInputStream();
        InputStreamReader isr = new InputStreamReader(is);
        BufferedReader br = new BufferedReader(isr);
        String line;
        while ((line = br.readLine()) != null) {
            oksb.append(line);
        }
        boolean success = process.waitFor(5, TimeUnit.SECONDS);
        if (success == false) {
            ctx.directError(null, "Fail to wait for keytool");
        } else {
            int exitValue = process.exitValue();
            if (exitValue == 0) {
                oksb.append("\nSuccess");
                ctx.directInfo(null, oksb.toString());
            } else {
                ctx.directError(null, oksb.toString());
                ctx.directError(null, "Failure executing keytool. ReturnCode: " + exitValue);
            }
        }
    } catch (Exception ex) {
        ctx.directError(null, "Failure executing keytool: " + ex.getMessage(), ex);
    }
}

From source file:jp.co.tis.gsp.tools.dba.util.ProcessUtil.java

public static void exec(Map<String, String> environment, String... args)
        throws IOException, InterruptedException {

    Process process = null;/*from  w  ww. j  a  va 2  s .  c  o m*/
    InputStream stdout = null;
    BufferedReader br = null;

    try {
        ProcessBuilder pb = new ProcessBuilder(args);
        pb.redirectErrorStream(true);
        if (environment != null) {
            pb.environment().putAll(environment);
        }

        process = pb.start();
        stdout = process.getInputStream();
        br = new BufferedReader(new InputStreamReader(stdout));
        String line;
        while ((line = br.readLine()) != null) {
            System.out.println(line);
        }
    } catch (IOException e) {
        throw e;
    } finally {
        IOUtils.closeQuietly(br);
        IOUtils.closeQuietly(stdout);

        if (process != null) {
            process.destroy();
        }
    }
}

From source file:Main.java

public static boolean isAndroidEmulatorRunning(File androidSdkHome) throws IOException {
    if (androidSdkHome == null) {
        return false;
    }//from w  ww .  j ava  2 s .c om

    File adb = new File(androidSdkHome, "platform-tools" + File.separator + "adb");
    if (!adb.exists()) {
        return false;
    }

    boolean isEmulatorRunning = false;

    ProcessBuilder pb = new ProcessBuilder(adb.getAbsolutePath(), "devices");
    pb.redirectErrorStream(true);
    Process p = pb.start();

    BufferedReader reader = new BufferedReader(new InputStreamReader(p.getInputStream()));
    String line = null;

    while ((line = reader.readLine()) != null) {
        line = line.trim();
        if (line.startsWith("List of devices")) {
            continue;
        } else if (line.startsWith("emulator-")) {
            String[] tokens = line.split("\\s");
            String name = tokens[0];
            String status = tokens[1];
            int port = Integer.parseInt(name.substring(name.indexOf("-") + 1));
            if (status.equals("device") && port == 5560) {
                isEmulatorRunning = true;
            }
        }
    }

    return isEmulatorRunning;

}

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

private static ExecutionResult runCpdWindows(Path tempDir, String... arguments) throws Exception {
    String cmd = tempDir.resolve(PMD_BIN_PREFIX + PMDVersion.VERSION + "/bin/cpd.bat").toAbsolutePath()
            .toString();/*from ww w  . ja v  a2 s. c o 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:com.frostwire.util.VPNs.java

private static String readProcessOutput(String command, String arguments) {
    String result = "";
    ProcessBuilder pb = new ProcessBuilder(command, arguments);
    pb.redirectErrorStream(true);
    try {/*from   ww w .j  a  v  a  2 s.  co m*/
        Process process = pb.start();
        InputStream stdout = process.getInputStream();
        final BufferedReader brstdout = new BufferedReader(new InputStreamReader(stdout));
        String line = null;

        try {
            StringBuilder stringBuilder = new StringBuilder();
            while ((line = brstdout.readLine()) != null) {
                stringBuilder.append(line);
            }

            result = stringBuilder.toString();
        } catch (Exception e) {
        } finally {
            IOUtils.closeQuietly(brstdout);
            IOUtils.closeQuietly(stdout);
        }

    } catch (Throwable e) {
        e.printStackTrace();
    }
    return result;
}