简体   繁体   中英

Process Builder doesnt accept input through getOutputStream

I am trying to run a windows command using process builder. I am not sure why it exits with 1 as i feel i gave the correct input. It would be great if anyone can look at it and guide where i might be wrong?

   try{
          ProcessBuilder pb = new ProcessBuilder("runas","/noprofile","/user:alex", "cmd");
          Process p = pb.start();
          OutputStream os = p.getOutputStream();
          PrintStream ps = new PrintStream(os);
          ps.println("password");
          ps.flush();

          System.out.println(p.waitFor());
          BufferedReader in = new BufferedReader(new InputStreamReader(p.getInputStream()));
          String resultLine = in.readLine();
          while (resultLine != null) {
              System.out.println(resultLine);
             resultLine = in.readLine();
          }
      }
           catch (IOException e) {
              e.printStackTrace();
          } 

Output:

1

Enter the password for alex:

Use this to get the error

                  System.out.println(p.waitFor());
                  BufferedReader in = new BufferedReader(new InputStreamReader(p.getErrorStream()));
                  String resultLine = in.readLine();
                  while (resultLine != null) {
                      resultLine = in.readLine();
                      System.out.println(resultLine);    
                     }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM