Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/windows/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java ProcessBuilder进程正在等待输入_Java_Windows_Process - Fatal编程技术网

Java ProcessBuilder进程正在等待输入

Java ProcessBuilder进程正在等待输入,java,windows,process,Java,Windows,Process,当通过ProcessBuilder(特别是“GetMac/s”)运行命令行命令时,如果它抛出错误或正常返回,我可以读取错误或它返回的MAC地址,但如果它提示用户输入(网络上的某些pc在使用GetMac时需要密码),则进程将挂起等待密码 以下是从命令行运行命令时的操作: 以下是我在流程中使用的代码: package testing; import java.io.IOException; class test1 { public static void main(String[] ar

当通过ProcessBuilder(特别是“GetMac/s”)运行命令行命令时,如果它抛出错误或正常返回,我可以读取错误或它返回的MAC地址,但如果它提示用户输入(网络上的某些pc在使用GetMac时需要密码),则进程将挂起等待密码

以下是从命令行运行命令时的操作:

以下是我在流程中使用的代码:

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的原因是为了在lan上唤醒程序,并希望在检测到任何新pc的mac地址时自动获取这些地址,这样用户就不必手动输入。因此,如果你知道一种更好的方法,让我知道,我会用它来代替远程pc的MAC

我意识到java可能不是最好的语言,但它是目前我唯一知道的语言,这只是一个有趣的小项目,可以让我在工作中休息


**编辑:如果它需要密码,我只想忽略这台电脑并终止进程,然后转到下一台电脑

您需要处理与进程相关的所有流,包括InputStream、ErrorStream和OutputStream。您在命令行上看到的文本将通过InputStream传递,然后您将希望通过OutputStream传递请求的信息

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


e、 g


如果我这样做,我仍然会得到同样的结果。这就好像输入密码的实际提示不是通过InputStream发送的。如果它可以在不需要密码的情况下获得MAC(它不会提示任何内容),它会将MAC打印到控制台。但是如果它确实需要密码,我无法知道,因为它不会打印(如果它需要密码,我只想忽略特定的pc并终止进程)@Orion:我的尝试被复制你的代码和保留你的
builder.inheritaio()行。如果你把它放在那里,流捕获就不起作用了。扔掉它,吞下你的溪流。
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);
      }      
   }
}