Java ProcessBuilder进程等待输入

时间:2014-09-16 21:05:33

标签: java windows process

当通过ProcessBuilder运行命令行命令(特别是" GetMac / s")如果它抛出错误或正常返回并且我可以读取它返回的错误或MAC地址但是如果它提示用户输入(使用getmac时,网络上的某些电脑需要密码),该过程将暂停等待密码。

以下是从命令行运行时该命令的作用: enter image description here

以下是我用于该流程的代码:

package testing;
import java.io.IOException;

class test1 {
    public static void main(String[] args){
        String hostName = "testpc";
        ProcessBuilder builder = new ProcessBuilder("getmac", "/s", hostName, "/nh");
        builder.inheritIO();
        try {
            Process proc = builder.start();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

我需要mac的原因是为了唤醒局域网程序,并希望在他们被检测到时自动获取任何新的PC的mac地址,这样用户就不必输入它手动。所以如果你知道一个更好的方法来获取远程电脑的MAC,请告诉我,我会改用它。

我意识到java可能不是最好用的语言,但它是我目前唯一知道的,这对于我的工作停机来说只是一个有趣的小项目。

**编辑:如果它需要密码我只想忽略该PC并终止进程并转到下一台PC

1 个答案:

答案 0 :(得分:3)

您需要处理与Process关联的所有Streams,包括InputStream,ErrorStream和OutputStream。您在命令行中看到的文本将通过InputStream传入,然后您将要传递通过OutputStream请求的信息。

您需要在自己的Threads中读取InputStream和ErrorStream。我经常将它们包装在Scanner对象中,如果它们正在传递文本,我经常将OutputStream包装在BufferedOutputStream中,并将其包装在PrintStream对象中。


如,

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintStream;
import java.util.Scanner;

class Test1 {
   private static PrintStream out;

   public static void main(String[] args) {
      String hostName = "testpc";
      String[] commands = {"getmac", "/s", hostName,
            "/nh"};
      ProcessBuilder builder = new ProcessBuilder(commands);

      // builder.inheritIO(); // I avoid this. It was messing me up.

      try {
         Process proc = builder.start();
         InputStream errStream = proc.getErrorStream();
         InputStream inStream = proc.getInputStream();
         OutputStream outStream = proc.getOutputStream();

         new Thread(new StreamGobbler("in", out, inStream)).start();
         new Thread(new StreamGobbler("err", out, errStream)).start();

         out = new PrintStream(new BufferedOutputStream(outStream));
         int errorCode = proc.waitFor();
         System.out.println("error code: " + errorCode);
      } catch (IOException e) {
         e.printStackTrace();
      } catch (InterruptedException e) {
         e.printStackTrace();
      } finally {
         if (out != null) {
            out.close();
         }
      }
   }
}

class StreamGobbler implements Runnable {
   private PrintStream out;
   private Scanner inScanner;
   private String name;

   public StreamGobbler(String name, PrintStream out, InputStream inStream) {
      this.name = name;
      this.out = out;
      inScanner = new Scanner(new BufferedInputStream(inStream));
   }

   @Override
   public void run() {
      while (inScanner.hasNextLine()) {
         String line = inScanner.nextLine();

         // do something with the line!
         // check if requesting password

         System.out.printf("%s: %s%n", name, line);
      }      
   }
}