将输入发送到进程

时间:2013-06-28 08:45:56

标签: java process runtime.exec

我有这个代码工作正常。如何将我的输入发送到Java Process Builder?

以下是代码:

PrintStream out = System.out;
Runtime rt = Runtime.getRuntime(); 
try {
     out.println("Executing a command in a separate process...");
     Scanner input = new Scanner(System.in);
     String str = input.nextLine();
     Process proc = rt.exec(str);

     /*OutputStream procIn = proc.getOutputStream();
     procIn.write("My Code".getBytes());
     procIn.close();*/

     InputStream procOut = proc.getInputStream();
     byte[] msgOut = new byte[64];
     int len = procOut.read(msgOut);
     procOut.close();
     out.println("Output from the command: "
        +new String(msgOut,0,len));

     out.println("Waiting for the process to finish...");
     int rc = proc.waitFor();
     out.println("Status code returned by the process "+rc);
  } catch (Exception e) {
     e.printStackTrace();
  }

我需要知道如何使用这一部分:

/*OutputStream procIn = proc.getOutputStream();
     procIn.write("My Code".getBytes());
     procIn.close();*/

提前感谢您的帮助。

0 个答案:

没有答案