5 Replies Latest reply: Dec 7, 2007 6:38 AM by 807601 RSS

    Handling the Process OutputStream

      Hello All,

      I'm writing a simple Java Editor, and I have problems in making my application's console the source of users' input .. For example; when using the Scanner, it writes from "System.in", how can I make this System.in writes the data from my application's console?

      This is how I handeld the process OutputStream:

      cmd[0] = "cmd.exe";
      cmd[1] = "/C";
      cmd[2] = "javaw";
      cmd[3] = "-cp";
      cmd[4] = filePath;
      cmd[5] = " "+fileName;
      Process p = runTime.exec(cmd);
      OutputStream os = p.getOutputStream();
      OutputStreamWriter oStreamw = new OutputStreamWriter(os); 
      BufferedWriter obw = new BufferedWriter(oStreamw);
        • 1. Re: Handling the Process OutputStream

          I don't even understand what you are trying to do with that code.
          • 2. Re: Handling the Process OutputStream
            I'm using the .exec(Array of Commands) to run any file written in Java.

            I want my Application to interactive, hence the Scanner.
            • 3. Re: Handling the Process OutputStream
              now i want to do the same, and can not get it done.

              i want to launch a java application with a Process instance.
              then i want to write the output of that process to a file.

              I have to make the data from the outputstream writing to a file...
              but how???
              • 4. Re: Handling the Process OutputStream
                Ok, I'm sorry, if I want to write the outputstream of a process to a file, I have to get it via Process.getInputStream())

                The api says clearly says for Process.getInputStream():
                "Gets the input stream of the subprocess. The stream obtains data piped from the standard output stream of the process represented by this Process object."

                But now another problem: it seems that the output of that process does not contain anything !!!

                How can I make that process write something to the stream that launches it?
                • 5. Re: Handling the Process OutputStream
                  I tried already this:

                  for the process that writes:
                  class program {
                       public static void main(String args[]) throws IOException {
                            OutputStream outstr;
                            BufferedOutputStream outbufstr;
                          Process process = Runtime.getRuntime().exec("cmd");   
                            outstr = process.getOutputStream();
                          outbufstr = new BufferedOutputStream(outstr);
                            String str = new String("this is a test");
                            byte[] b = str.getBytes();
                  for the calling process:
                  import java.io.*;
                  import java.io.File;
                  import java.io.IOException;
                  public class ProcTester {
                       public static void main (String args[]) throws IOException {
                            System.out.println("start 1");
                            ProcessBuilder pb = new ProcessBuilder("java","program");          
                            File f = new File("C:\\Temp\\200712071320_java_processstreamcomm");          
                            Process p = pb.start();          
                            long waiter=0;
                            BufferedInputStream bis = new BufferedInputStream(p.getInputStream());
                            int nextByte;
                            StringBuffer localBuffer = new StringBuffer( );
                            //while( (-1 != (nextByte = bis.read( ))) || (waiter <= 99999)) {
                            while(waiter <= 999999) {
                                 if ( -1 != (nextByte = bis.read())) {
                                      char nextChar = (char) nextByte;
                            System.out.println("end 1");
                  But no success...
                  I don't see any output in my calling program.

                  What's wrong????