title: Using ProcessBuilder to read/write to telnet process
body: <p>I am trying to read/write values from/to <code>telnet</code> process by means of <code>ProcessBuilder</code>. </p>
  
  <pre><code> public static void main(String[] args) {
          try {
              telnetProcess = new ProcessBuilder("C:\\Windows\\System32\\telnet.exe","x.x.x.x").start();
              telnetInputReader = new BufferedReader(new InputStreamReader(telnetProcess.getInputStream()));
              telnetOuputWriter = new BufferedWriter(new OutputStreamWriter(telnetProcess.getOutputStream()));
              expectPattern("login:");
              sendCmd("user");
              expectPattern("password:");
              sendCmd("pwd");
              expectPattern("switch&gt;#");
              sendCmd("exit");
              expectPattern("Connection to host lost");
          } catch (IOException ex) {
              System.out.println("Exception : " + ex);
          }
      }
  </code></pre>
  
  <p>I got the following error </p>
  
  <pre><code>java.io.IOException: Cannot run program "C:\Windows\System32\telnet.exe": CreateProcess error=2, The system cannot find the file specified
  </code></pre>
  
  <p>I tried to change the file path to unix formatted style like <code>C:/Windows/System32/telnet.exe</code> and no luck. (Though I expected it to not to work). Then copied the <code>telnet.exe</code> from it's location to some other user's home directory and I was not getting any errors. (???)</p>
  
  <p>But, I didn't see the output as expected. I didn't get any response from the process and the code exited. </p>
  
  <pre><code>    public static void sendCmd(String cmd) {
          System.out.println(cmd);
          try {
              telnetOuputWriter.write(cmd + "\n", 0, cmd.length());
          } catch (IOException ex) {
              Logger.getLogger(TelnetProcessHandler.class.getName()).log(Level.SEVERE, null, ex);
          }
  
      }
  
      public static String expectPattern(String pattern) {
          String cmdResponse = "";
          try {
              String line = "";
              // Always getting 'null' here
              while ((line = telnetInputReader.readLine()) != null) {
                  System.out.println(line);
                  cmdResponse += line;
                  if (line.contains(pattern)) {
                      break;
                  }
              }
          } catch (IOException ex) {
              System.out.println("ex : " + ex);
          }
          return cmdResponse;
      }
  </code></pre>
  
  <p>What is wrong in this ?  Then, one other query. I have tried using <code>PrintWriter</code> for writing to process which in turn has <code>BufferedWriter</code> in it, like, </p>
  
  <pre><code>telnetOuputWriter = new PrintWriter(new BufferedWriter(new OutputStreamWriter(telnetProcess.getOutputStream())));
  </code></pre>
  
  <p>Is this fine to use <code>PrintWriter</code> in this context ? </p>
  
  <p><strong>Note :</strong> Due to some reasons, I would like to stick with using telnet by means of process, not with <code>Socket</code> or <code>TelnetClient</code> or <code>expect4j</code>.</p>