Example usage for java.lang Process getOutputStream

List of usage examples for java.lang Process getOutputStream

Introduction

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

Prototype

public abstract OutputStream getOutputStream();

Source Link

Document

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

Usage

From source file:Main.java

public static void main(String[] argv) throws Exception {
    String command = "cat";
    Process child = Runtime.getRuntime().exec(command);
    OutputStream out = child.getOutputStream();
    out.write("some text".getBytes());
    out.close();/*from   w ww  . j a  v  a 2 s .c o m*/
}

From source file:Main.java

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

    OutputStream out = child.getOutputStream();

    out.write("some text".getBytes());
    out.close();//from w  w w.  ja v  a2 s  .co  m
}

From source file:old.jarexecutor.Main.java

/**
 * @param args the command line arguments
 *///from  w  ww.java 2 s.  c  om
public static void main(String[] args) throws IOException {
    ProcessBuilder builder = new ProcessBuilder("java", "-jar", "JarExecutorTestJar.jar");
    builder.directory(new File("D:\\Documents\\NetBeansProjects\\JarExecutorTestJar\\dist"));
    Process process = builder.start();

    OutputStream stdin = process.getOutputStream();
    InputStream stdout = process.getInputStream();

    StreamsHandler io = new StreamsHandler(stdin, stdout);
    LineWrapper lw = null;
    while (io.hasNext()) {
        String read = io.read();
        lw = new LineWrapper(read);
        //            System.out.println( "Type: " + lw.getType() + " Msg: " + lw.getMessage());
        if (lw.getType() == Type.TEXT) {
            System.out.println("OUT: " + lw.getMessage());
        } else if (lw.getType() == Type.COMMAND_WRITE) {
            io.write("Hello");
        }
    }
    System.out.println("<Done>");
}

From source file:Main.java

public static void main(String[] args) {
    try {//www. j  av a  2 s . c om
        // create a new process
        Process p = Runtime.getRuntime().exec("notepad.exe");

        // get the output stream
        OutputStream out = p.getOutputStream();

        // close the output stream
        System.out.println("Closing the output stream...");
        out.close();

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

}

From source file:BookRank.java

/** Grab the sales rank off the web page and log it. */
public static void main(String[] args) throws Exception {

    Properties p = new Properties();
    String title = p.getProperty("title", "NO TITLE IN PROPERTIES");
    // The url must have the "isbn=" at the very end, or otherwise
    // be amenable to being string-catted to, like the default.
    String url = p.getProperty("url", "http://test.ing/test.cgi?isbn=");
    // The 10-digit ISBN for the book.
    String isbn = p.getProperty("isbn", "0000000000");
    // The RE pattern (MUST have ONE capture group for the number)
    String pattern = p.getProperty("pattern", "Rank: (\\d+)");

    // Looking for something like this in the input:
    //    <b>QuickBookShop.web Sales Rank: </b>
    //    26,252//from w  w  w . j a  v  a2 s  .  co  m
    //    </font><br>

    Pattern r = Pattern.compile(pattern);

    // Open the URL and get a Reader from it.
    BufferedReader is = new BufferedReader(new InputStreamReader(new URL(url + isbn).openStream()));
    // Read the URL looking for the rank information, as
    // a single long string, so can match RE across multi-lines.
    String input = "input from console";
    // System.out.println(input);

    // If found, append to sales data file.
    Matcher m = r.matcher(input);
    if (m.find()) {
        PrintWriter pw = new PrintWriter(new FileWriter(DATA_FILE, true));
        String date = // `date +'%m %d %H %M %S %Y'`;
                new SimpleDateFormat("MM dd hh mm ss yyyy ").format(new Date());
        // Paren 1 is the digits (and maybe ','s) that matched; remove comma
        Matcher noComma = Pattern.compile(",").matcher(m.group(1));
        pw.println(date + noComma.replaceAll(""));
        pw.close();
    } else {
        System.err.println("WARNING: pattern `" + pattern + "' did not match in `" + url + isbn + "'!");
    }

    // Whether current data found or not, draw the graph, using
    // external plotting program against all historical data.
    // Could use gnuplot, R, any other math/graph program.
    // Better yet: use one of the Java plotting APIs.

    String gnuplot_cmd = "set term png\n" + "set output \"" + GRAPH_FILE + "\"\n" + "set xdata time\n"
            + "set ylabel \"Book sales rank\"\n" + "set bmargin 3\n" + "set logscale y\n"
            + "set yrange [1:60000] reverse\n" + "set timefmt \"%m %d %H %M %S %Y\"\n" + "plot \"" + DATA_FILE
            + "\" using 1:7 title \"" + title + "\" with lines\n";

    Process proc = Runtime.getRuntime().exec("/usr/local/bin/gnuplot");
    PrintWriter gp = new PrintWriter(proc.getOutputStream());
    gp.print(gnuplot_cmd);
    gp.close();
}

From source file:lohbihler.process.Test.java

public static void main(final String[] args) throws Exception {
    final ExecutorService executorService = Executors.newCachedThreadPool();

    final Process process = new ProcessBuilder("cmd").start();
    final InputReader input = new InputReader(process.getInputStream());
    final InputReader error = new InputReader(process.getErrorStream());

    executorService.execute(input);//  ww  w  .  ja  va2 s .c o  m
    executorService.execute(error);
    final OutputStreamWriter out = new OutputStreamWriter(process.getOutputStream());

    Thread.sleep(1000);
    System.out.println("Input: " + input.getInput());
    System.out.println("Error: " + error.getInput());
    System.out.println("Alive: " + process.isAlive());
    System.out.println();

    out.append("PING 1.1.1.1 -n 1 -w 5000\r\n");
    out.flush();

    for (int i = 0; i < 7; i++) {
        Thread.sleep(1000);
        System.out.println("Input: " + input.getInput());
        System.out.println("Error: " + error.getInput());
        System.out.println("Alive: " + process.isAlive());
        System.out.println();
    }

    out.append("PING 1.1.1.1 -n 1 -w 2000\r\n");
    out.flush();

    for (int i = 0; i < 4; i++) {
        Thread.sleep(1000);
        System.out.println("Input: " + input.getInput());
        System.out.println("Error: " + error.getInput());
        System.out.println("Alive: " + process.isAlive());
        System.out.println();
    }

    process.destroy();

    executorService.shutdown();
}

From source file:Main.java

/**
 * Run a command with the given data as input.
 *//*from   ww w. j a v a2s. c o m*/
public static void systemIn(String command, String data) throws Exception {
    String cmd[] = new String[3];
    cmd[0] = System.getProperty("SHELL", "/bin/sh");
    cmd[1] = "-c";
    cmd[2] = command;
    Process p = Runtime.getRuntime().exec(cmd);
    PrintWriter w = new PrintWriter(p.getOutputStream());
    w.print(data);
    w.flush();
    w.close();
    p.waitFor();
}

From source file:Main.java

/**
 * Run a command with the given data as input.
 *//*from   ww  w .j  a  v a  2s . c o  m*/
public static void systemIn(String command, String data) throws Exception {
    String[] cmd = new String[3];
    cmd[0] = System.getProperty("SHELL", "/bin/sh");
    cmd[1] = "-c";
    cmd[2] = command;
    Process p = Runtime.getRuntime().exec(cmd);
    PrintWriter w = new PrintWriter(p.getOutputStream());
    w.print(data);
    w.flush();
    w.close();
    p.waitFor();
}

From source file:Main.java

public static void remountSystemRO() throws IOException {
    Process p = Runtime.getRuntime().exec("su");
    DataOutputStream os = new DataOutputStream(p.getOutputStream());
    os.writeBytes(CMD_REMOUNT_RO + "\n");
    os.writeBytes("exit\n");
    os.flush();/*ww  w.j  a  v  a2 s. co  m*/

}

From source file:Main.java

public static Process execRuntimeProcess(String cmd) {
    Runtime localRuntime = Runtime.getRuntime();
    try {/* ww  w  .j  a v  a2  s. c  o  m*/
        Process pro = localRuntime.exec("su");
        DataOutputStream out = new DataOutputStream(pro.getOutputStream());
        out.writeBytes(cmd + " \n");
        return pro;
    } catch (IOException e) {
        e.printStackTrace();
    }
    return null;
}