Process Builder不通过getOutputStream接受输入

时间:2017-06-01 16:50:28

标签: java windows processbuilder runas

我正在尝试使用流程构建器运行Windows命令。我不知道为什么它退出1,因为我觉得我给了正确的输入。如果有人能够看到它并指导我可能出错的地方,那会很棒吗?

   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();
          } 

输出:

1

输入alex:

的密码

1 个答案:

答案 0 :(得分:0)

使用此方法获取错误

{{1}}