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:Main.java

public static void main(String args[]) throws IOException {

    Runtime runtime = Runtime.getRuntime();
    Process process = runtime.exec(args);
    InputStream is = process.getInputStream();
    InputStreamReader isr = new InputStreamReader(is);
    BufferedReader br = new BufferedReader(isr);
    String line;//from   w  ww.  jav  a2 s.  com

    System.out.printf("Output of running %s is:", Arrays.toString(args));

    while ((line = br.readLine()) != null) {
        System.out.println(line);
    }

}

From source file:Main.java

public static void main(String[] argv) throws Exception {
    String command = "ls";
    Process child = Runtime.getRuntime().exec(command);

    InputStream in = child.getInputStream();
    int c;//from  w w  w. java  2 s .  c o  m
    while ((c = in.read()) != -1) {
        System.out.println(((char) c));
    }
    in.close();
}

From source file:Main.java

public static void main(String[] argv) throws Exception {
    String command = "ls";
    Process child = Runtime.getRuntime().exec(command);

    InputStream in = child.getInputStream();
    int c;//w w w. ja  va 2s .  c o  m
    while ((c = in.read()) != -1) {
        System.out.println((char) c);
    }
    in.close();
}

From source file:Run2.java

public static void main(String[] args) throws java.io.IOException {
    if (args.length != 1) {
        System.err.println("usage: java Run pathname");
        return;//from  w  w w  . j  av  a  2  s .co  m
    }

    Process p = Runtime.getRuntime().exec(args[0]);

    InputStream is = p.getInputStream();
    int b;
    while ((b = is.read()) != -1)
        System.out.print((char) b);

    try {
        System.out.println("Exit status = " + p.waitFor());
    } catch (InterruptedException e) {
    }
}

From source file:Main.java

public static void main(String args[]) throws IOException {

    Process process = new ProcessBuilder(args).start();
    InputStream is = process.getInputStream();
    InputStreamReader isr = new InputStreamReader(is);
    BufferedReader br = new BufferedReader(isr);
    String line;//from   w  w  w  . j  a v a  2  s . c o m

    System.out.printf("Output of running %s is:", Arrays.toString(args));

    while ((line = br.readLine()) != null) {
        System.out.println(line);
    }

}

From source file:ExecShellArgs.java

public static void main(String[] args) throws IOException {
    Runtime r = Runtime.getRuntime();
    String[] nargs = { "sh", "-c", "for i in 1 2 3; do echo $i; done" };
    Process p = r.exec(nargs);
    BufferedReader is = new BufferedReader(new InputStreamReader(p.getInputStream()));
    String line;//from www  .  j ava 2 s. c  om
    while ((line = is.readLine()) != null)
        System.out.println(line);
}

From source file:MainClass.java

public static void main(String[] args) throws Exception {
    Runtime r = Runtime.getRuntime();
    String[] nargs = { "sh", "-c", "for i in 1 2 3; do echo $i; done" };
    Process p = r.exec(nargs);
    BufferedReader is = new BufferedReader(new InputStreamReader(p.getInputStream()));
    String line;/*from w  ww  . j a v  a2  s  .com*/
    while ((line = is.readLine()) != null)
        System.out.println(line);
}

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);//from  w w  w  . ja  v  a2  s . co m
    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;
    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:Main.java

public static void main(String[] args) {
    try {/*from   w  w  w.  j  a va  2s. c o  m*/
        // create a new process
        Process p = Runtime.getRuntime().exec("notepad.exe");

        // get the input stream of the process and print it
        InputStream in = p.getInputStream();
        for (int i = 0; i < in.available(); i++) {
            System.out.println(in.read());
        }

        // wait for 10 seconds and then destroy the process
        Thread.sleep(10000);
        p.destroy();

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

}

From source file:Main.java

public static void main(String argv[]) throws Exception {
    String line;// w w  w  .  j a  v  a 2 s .  c o  m
    Process p = Runtime.getRuntime().exec("psql -U username -d dbname -h serverhost -f scripfile.sql");
    BufferedReader input = new BufferedReader(new InputStreamReader(p.getInputStream()));
    while ((line = input.readLine()) != null) {
        System.out.println(line);
    }
    input.close();
}