Example usage for java.lang Process getInputStream

List of usage examples for java.lang Process getInputStream

Introduction

In this page you can find the example usage for java.lang Process getInputStream.

Prototype

public abstract InputStream getInputStream();

Source Link

Document

Returns the input stream connected to the normal output of the process.

Usage

From source file:uk.ac.ebi.eva.test.utils.JobTestUtils.java

public static void restoreMongoDbFromDump(String dumpLocation) throws IOException, InterruptedException {
    logger.info("restoring DB from " + dumpLocation);

    Process exec = Runtime.getRuntime().exec("mongorestore " + dumpLocation);
    exec.waitFor();//from  w ww.j  a va  2 s . co  m
    String line;
    BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(exec.getInputStream()));
    while ((line = bufferedReader.readLine()) != null) {
        logger.info("mongorestore output:" + line);
    }
    bufferedReader.close();
    bufferedReader = new BufferedReader(new InputStreamReader(exec.getErrorStream()));
    while ((line = bufferedReader.readLine()) != null) {
        logger.info("mongorestore errorOutput:" + line);
    }
    bufferedReader.close();

    logger.info("mongorestore exit value: " + exec.exitValue());
}

From source file:ca.uqac.dim.net.verify.NetworkChecker.java

/**
 * Runs the NuSMV program as a spawned command-line process, and passes the
 * file to process through that process' standard input
 * @param file_contents A String containing the NuSMV model to process
 *//*from  w w  w . ja v a 2 s  .c  om*/
private static RuntimeInfos runNuSMV(String file_contents) throws IOException, InterruptedException {
    StringBuilder sb = new StringBuilder();
    Runtime rt = Runtime.getRuntime();
    long time_start = 0, time_end = 0;
    // Start NuSMV, and feed the model through its standard input
    time_start = System.currentTimeMillis();
    Process p = rt.exec(NUSMV_EXEC);
    OutputStream o = p.getOutputStream();
    InputStream i = p.getInputStream();
    InputStream e = p.getErrorStream();
    Writer w = new PrintWriter(o);
    w.write(file_contents);
    w.close(); // Close stdin so NuSMV can start processing it
    // Wait for NuSMV to be done, then collect its standard output
    int exitVal = p.waitFor();
    if (exitVal != 0)
        throw new IOException("NuSMV's return value indicates an error in processing its input");
    BufferedReader br = new BufferedReader(new InputStreamReader(i));
    String line;
    while ((line = br.readLine()) != null) {
        sb.append(line).append("\n");
    }
    time_end = System.currentTimeMillis();
    i.close(); // Close stdout
    e.close(); // Close stderr
    return new RuntimeInfos(sb.toString(), time_end - time_start);
}

From source file:com.liferay.blade.tests.BladeCLI.java

public static String execute(File workingDir, String... bladeArgs) throws Exception {

    String bladeCLIJarPath = getLatestBladeCLIJar();

    List<String> command = new ArrayList<>();

    command.add("java");
    command.add("-jar");
    command.add(bladeCLIJarPath);/*from  w ww  .  j  ava2  s .co m*/

    for (String arg : bladeArgs) {
        command.add(arg);
    }

    Process process = new ProcessBuilder(command.toArray(new String[0])).directory(workingDir).start();

    process.waitFor();

    InputStream stream = process.getInputStream();

    String output = new String(IO.read(stream));

    InputStream errorStream = process.getErrorStream();

    String errors = new String(IO.read(errorStream));

    assertTrue(errors, errors == null || errors.isEmpty());

    output = StringUtil.toLowerCase(output);

    return output;
}

From source file:com.liferay.blade.tests.BladeCLI.java

public static String startServerWindows(File workingDir, String... bladeArgs) throws Exception {

    String bladeCLIJarPath = getLatestBladeCLIJar();

    List<String> command = new ArrayList<>();

    command.add("start");
    command.add("/b");
    command.add("java");
    command.add("-jar");
    command.add(bladeCLIJarPath);/* ww w  . j av  a  2 s .com*/

    for (String arg : bladeArgs) {
        command.add(arg);
    }

    Process process = new ProcessBuilder(command.toArray(new String[0])).directory(workingDir).start();

    process.waitFor();

    InputStream stream = process.getInputStream();

    String output = new String(IO.read(stream));

    InputStream errorStream = process.getErrorStream();

    String errors = new String(IO.read(errorStream));

    assertTrue(errors, errors == null || errors.isEmpty());

    return output;
}

From source file:Main.java

public static int shellExecute(String strCommand, boolean bDisplayProgramOutput) {
    Process p = null;
    try {/*ww w  . ja  v  a2s .  c o  m*/
        p = Runtime.getRuntime().exec(strCommand);
    } catch (IOException e) {
        e.printStackTrace();
    }

    if (p != null) {
        BufferedReader input = new BufferedReader(new InputStreamReader(p.getInputStream()));
        String line = "";
        try {
            line = input.readLine();
            if (bDisplayProgramOutput && line != null)
                System.out.println(line);
        } catch (IOException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        }
        while (line != null) {
            try {
                line = input.readLine();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            if (bDisplayProgramOutput && line != null)
                System.out.println(line);
        }

        try {
            p.waitFor();
        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        return p.exitValue();
    }

    return -1;
}

From source file:com.amazonaws.services.iot.demo.danbo.rpi.Danbo.java

public static String getSerialNumber() throws Exception {
    Process p = Runtime.getRuntime().exec("cat /proc/cpuinfo");

    BufferedReader stdInput = new BufferedReader(new InputStreamReader(p.getInputStream()));

    String s = "";
    String serial = "";

    // read the output from the command
    while ((s = stdInput.readLine()) != null) {
        if (s.contains("Serial")) {
            serial = s.split(":")[1].trim().substring(8, 16);
        }//w ww .  j a  v  a 2 s  .c o m
    }

    return serial;
}

From source file:com.samczsun.helios.utils.Utils.java

public static String readProcess(Process process) {
    StringBuilder result = new StringBuilder();
    result.append("--- BEGIN PROCESS DUMP ---").append("\n");
    result.append("---- STDOUT ----").append("\n");
    InputStream inputStream = process.getInputStream();
    byte[] inputStreamBytes = new byte[0];
    try {/*  w w  w  . ja  v a 2s.co m*/
        inputStreamBytes = IOUtils.toByteArray(inputStream);
    } catch (IOException e) {
        result.append("An error occured while reading from stdout").append("\n");
        result.append("Caused by: ").append(e.getClass()).append(" ").append(e.getMessage()).append("\n");
    } finally {
        if (inputStreamBytes.length > 0) {
            result.append(new String(inputStreamBytes, StandardCharsets.UTF_8));
        }
    }
    result.append("---- STDERR ----").append("\n");
    inputStream = process.getErrorStream();
    inputStreamBytes = new byte[0];
    try {
        inputStreamBytes = IOUtils.toByteArray(inputStream);
    } catch (IOException e) {
        result.append("An error occured while reading from stderr").append("\n");
        result.append("Caused by: ").append(e.getClass()).append(" ").append(e.getMessage()).append("\n");
    } finally {
        if (inputStreamBytes.length > 0) {
            result.append(new String(inputStreamBytes, StandardCharsets.UTF_8));
        }
    }

    result.append("---- EXIT VALUE ----").append("\n");

    int exitValue = -0xCAFEBABE;
    try {
        exitValue = process.waitFor();
    } catch (InterruptedException e) {
        result.append("An error occured while obtaining the exit value").append("\n");
        result.append("Caused by: ").append(e.getClass()).append(" ").append(e.getMessage()).append("\n");
    } finally {
        if (exitValue != -0xCAFEBABE) {
            result.append("Process finished with exit code ").append(exitValue).append("\n");
        }
    }

    return result.toString();
}

From source file:de.uni.bremen.monty.moco.Main.java

private static void runExecutable(File executable) throws IOException, InterruptedException {
    ProcessBuilder processBuilder = new ProcessBuilder(executable.getAbsolutePath());

    String readFromFile = System.getProperty("testrun.readFromFile");
    if (readFromFile != null) {
        processBuilder.redirectInput(new File(readFromFile));
    } else {// w  ww  .j av  a 2  s . co m
        processBuilder.redirectInput(ProcessBuilder.Redirect.INHERIT);
    }
    Process process = processBuilder.start();
    System.err.print(IOUtils.toString(process.getErrorStream()));
    System.out.print(IOUtils.toString(process.getInputStream()));
}

From source file:de.uni.bremen.monty.moco.Main.java

private static void runCode(File llvmCode) throws IOException {
    ProcessBuilder processBuilder = new ProcessBuilder("lli", llvmCode.getAbsolutePath());
    String readFromFile = System.getProperty("testrun.readFromFile");
    if (readFromFile == null) {
        processBuilder.redirectInput(ProcessBuilder.Redirect.INHERIT);
    } else {//from w w w .  j a  v  a  2 s . c o m
        processBuilder.redirectInput(new File(readFromFile));
    }
    Process process = processBuilder.start();

    System.err.print(IOUtils.toString(process.getErrorStream()));
    System.out.print(IOUtils.toString(process.getInputStream()));
}

From source file:edu.brown.benchmark.voteresper.EPRuntimeUtil.java

public static String executeCommand(String command) {

    StringBuffer output = new StringBuffer();

    Process p;
    try {//from   w w w . ja  v  a2  s.  com
        p = Runtime.getRuntime().exec(command);
        p.waitFor();
        BufferedReader reader = new BufferedReader(new InputStreamReader(p.getInputStream()));

        String line = "";
        while ((line = reader.readLine()) != null) {
            output.append(line + "\n");
        }

    } catch (Exception e) {
        e.printStackTrace();
    }

    return output.toString();

}