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

    Handling the Process OutputStream

    807599
      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);
      obw.write(MainWindow.console.getText(),0,3);
      obw.close();
        • 1. Re: Handling the Process OutputStream
          791266
          What?

          I don't even understand what you are trying to do with that code.
          • 2. Re: Handling the Process OutputStream
            807599
            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
              807601
              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
                807601
                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
                  807601
                  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();
                            
                            outbufstr.write(b);
                            outbufstr.flush();     
                  
                            outbufstr.write(b);
                            outbufstr.flush();     
                            
                       }
                  
                  }
                  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");          
                            pb.directory(f);
                            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) {
                                 waiter++;
                                 if ( -1 != (nextByte = bis.read())) {
                                      char nextChar = (char) nextByte;
                                      localBuffer.append(nextChar);
                                 }
                            }
                            System.out.println(localBuffer.toString());
                            System.out.println("end 1");
                            
                       }
                       
                  }
                  But no success...
                  I don't see any output in my calling program.

                  What's wrong????