java管道/进程构建器不使用cmd.exe

时间:2012-05-28 18:52:22

标签: java process piping

这是我的代码,它只是打开命令提示符Windows 7然后坐下。没有其他的。我希望它显然发送和接收命令。所以什么错了?

String line;
try {
    Process p = Runtime.getRuntime().exec("cmd /c start cmd.exe");
    BufferedReader inp =
        new BufferedReader(
            new InputStreamReader(p.getInputStream()));
    BufferedWriter out =
        new BufferedWriter(
            new OutputStreamWriter(p.getOutputStream()));
    out.append("sometext");
    out.write("Some Text!\n\n");
    out.flush();
    line = inp.readLine();
    System.out.println("response1: " + line );   // that's ok
    out.write("Second Line...\n");
    out.flush();
    line = inp.readLine();
    // returns an empty string, if it returns...
    System.out.println("response2: " + line);    
    inp.close();
    out.close();
} catch (IOException io) {

}

2 个答案:

答案 0 :(得分:2)

cmd start将启动一个新的命令提示符窗口,您的输入和输出缓冲区将不会连接到它。

答案 1 :(得分:1)

您可能希望在不同的线程上异步执行这些操作,并且您绝对不希望忽略异常或错误流。

但最重要的是,您没有正确调用cmd,因为通过执行“cmd / c start cmd.exe”,您将在进程中创建进程。

例如,

import java.io.*;

public class OpenCmd {
   public static void main(String[] args) {
      try {
         // Process p = Runtime.getRuntime().exec("cmd /c start cmd.exe");
         Process p = Runtime.getRuntime().exec("cmd.exe");
         final BufferedReader inp = new BufferedReader(new InputStreamReader(
               p.getInputStream()));
         final BufferedWriter out = new BufferedWriter(new OutputStreamWriter(
               p.getOutputStream()));
         final BufferedReader err = new BufferedReader(new InputStreamReader(
               p.getErrorStream()));

         new Thread(new Runnable() {
            public void run() {
               try {
                  out.append("sometext");
                  out.write("Some Text!\n\n");
                  out.flush();
                  out.write("Second Line...\n");
                  out.flush();
               } catch (IOException e) {
                  e.printStackTrace();
               } finally {
                  try {
                     out.close();
                  } catch (IOException e) {
                     e.printStackTrace();
                  }
               }
            }
         }).start();
         new Thread(new Runnable() {
            public void run() {
               try {
                  String line = "";
                  while ((line = inp.readLine()) != null) {
                     System.out.println("response1: " + line);
                  }

               } catch (IOException e) {
                  e.printStackTrace();
               } finally {
                  try {
                     inp.close();
                  } catch (IOException e) {
                     e.printStackTrace();
                  }
               }
            }
         }).start();
         new Thread(new Runnable() {
            public void run() {
               try {
                  String line = "";
                  while ((line = err.readLine()) != null) {
                     System.err.println("err: " + line);
                  }
                  inp.close();
               } catch (IOException e) {
                  e.printStackTrace();
               }
            }
         }).start();
         int exitVal = p.waitFor();
         System.out.println("exitVal := " + exitVal);

      } catch (IOException io) {
         io.printStackTrace();
      } catch (InterruptedException e) {
         // TODO Auto-generated catch block
         e.printStackTrace();
      }
   }
}