Java ProcessBuilder process waiting for input

后端 未结 1 1493
旧巷少年郎
旧巷少年郎 2020-12-22 03:35

When running a command line command via ProcessBuilder (specifically \"GetMac /s\") if it throws an error or returns normally and I can read the error or the MAC address tha

1条回答
  •  有刺的猬
    2020-12-22 04:25

    You need to handle all the Streams associated with the Process, including the InputStream, ErrorStream and OutputStream. The text you see on the command line will be coming through the InputStream, and you'll then want to pass information requested through the OutputStream.

    You'll want to read the InputStream and the ErrorStream both in their own Threads. I often wrap them in a Scanner object if they're passing text, and I often wrap the OutputStream in a BufferedOutputStream, and that in a PrintStream object.


    e.g.,

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

    0 讨论(0)
提交回复
热议问题