4 Replies Latest reply: Jan 24, 2014 3:19 PM by rp0428 RSS

    cannot read the output from windows command.

    Aurelian

      Hello

       

      I have the following classes

      {

      package cmd;
      
      import java.io.IOException;
      
      public class CMD {
          
          public CMD(){
          
              ProcessBuilder pb = new ProcessBuilder()
              .command("cmd.exe","/c","del *.*")
              .redirectErrorStream(false);
              Process p;
              try {
                  p = pb.start();
                  StreamGobbler errorGobbler = new StreamGobbler(p.getErrorStream(), "ERROR");
      
                  // any output?
                  StreamGobbler outputGobbler = new StreamGobbler(p.getInputStream(), "OUTPUT");
      
                  // start gobblers
                  outputGobbler.start();
                  errorGobbler.start();
      
              } catch (IOException e) {
                  // TODO Auto-generated catch block
              System.out.println("eee "+e.getMessage());;
              }    }
      
          
          public static void main(String[] args) {
              System.out.println("x");
              new CMD();
              System.out.println("x");
          }
      }
      

      }

      and

      package cmd;
      
      import java.io.BufferedReader;
      import java.io.IOException;
      import java.io.InputStream;
      import java.io.InputStreamReader;
      
       public class StreamGobbler extends Thread {
          InputStream is;
          String type;
      
          StreamGobbler(InputStream is, String type) {
              this.is = is;
              this.type = type;
          }
      
          @Override
          public void run() {
              try {
                  InputStreamReader isr = new InputStreamReader(is);
                  BufferedReader br = new BufferedReader(isr);
                  String line = null;
                  while ((line = br.readLine()) != null)
                      System.out.println(type + "> " + line);
              }
              catch (IOException ioe) {
                  ioe.printStackTrace();
              }
          }
      }
      

       

      Please note that I cannot seee the output from wndows command : del *.* and the java class execution does not finished.

      I I will replace the above command with the dir command then the output of the command is visible.

      Would you give me a hint about how to modify the above clases in order to parse the output of the del *.* ?

      Please note that the above example is important because I am developping a tool and it is mandatory for that tool to parse the output from a windows batch command.


      Best regards,

        • 1. Re: cannot read the output from windows command.
          rp0428
          Please note that I cannot seee the output from wndows command : del *.* and the java class execution does not finished.

          I I will replace the above command with the dir command then the output of the command is visible.

          Would you give me a hint about how to modify the above clases in order to parse the output of the del *.* ?

          No - but I will give you a hint about ProcessBuilder and how to develop software properly.

           

          Hint #1: Don't try to automate something that you don't know, or understand, how to do manually.

           

          a. Do you know how to execute 'del *.*' manually in a command window?

          b. Did you try that manually to see what happens?

           

          My guess is 'no'. If you had you would know that the response to a 'del *.*' command is going to be this:

          Are you sure (Y/N)?

          And your 'java class execution' doesn't finish because the 'del' command is waiting for you to answer that question.

           

          Hint #2: Don't try to use ProcessBuilder for an application that requires console input unless you first know how to provide that console input via your Java code.

           

          Your code will wait forever since it does NOT answer that question.

           

          Search the net and The Java Tutorials and  you can find examples of executing command line utilities. Then try those examples first and make sure that:

           

          1. They work for you

          2. You understand HOW they work

           

          Then you can modify those examples to do what you want to do.

          • 2. Re: cannot read the output from windows command.
            Aurelian

            Hello rp0428

             

            Regarding your questions:

            a. Do you know how to execute 'del *.*' manually in a command window?

            YES

            b. Did you try that manually to see what happens?

            YES

            Your code will wait forever since it does NOT answer that question.[I know that but i cannot see the question in my console.]

             

            Regarding reading tutorials.

            Please give me a valid answer point me a good tutorial that reasolve my problem if you know.

             

            Regarding the following advices:

            {

            Search the net and The Java Tutorials and  you can find examples of executing command line utilities. Then try those examples first and make sure that:

             

            1. They work for you

            2. You understand HOW they work

            }

            1. They do not work for my problem

            2 I understood how they work.

            • 3. Re: cannot read the output from windows command.
              gimbal2

              No, YOU need to resolve your problem, not some tutorial. Don't try to outsource responsibility.

               

              I would rather investigate the Javadoc of the Process class to see how you might transmit input to a process. You know how to get output from a command, it shouldn't be too much of a leap for you to figure out what to look for to send input to it.

               

              Google: java 7 Process

              Result: Process (Java Platform SE 7 )

              • 4. Re: cannot read the output from windows command.
                rp0428

                Regarding your questions:

                a. Do you know how to execute 'del *.*' manually in a command window?

                YES

                Sorry - you got that one wrong.

                 

                I both told you, and showed you, what the problem is with the command you are trying to execute.

                 

                You would SEE that is the problem if you manually try to execute 'del *.*' in a command window.

                 

                And if you type 'help del' in a command window you would see what command line switch to use to AVOID that problem.

                 

                SIMPLE IS BETTER! Quit trying to make things more complicated than they are. As I said before:

                Hint #1: Don't try to automate something that you don't know, or understand, how to do manually.

                Your code is NOT doing what YOU are doing manually. When I make that one simple change to the 'del' command (that 'help del' will show you) your code executes just fine.