2012-05-28 53 views
1

工作這裏它只是打開一個命令提示符窗口7,然後坐在我的代碼。沒有其他的。我希望它明顯地發送和接收命令。所以最新錯誤?的java管/進程生成不CMD.EXE

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) { 

} 
+0

請參閱編輯回答。 –

回答

1

您可能希望在不同線程上異步執行這些操作,並且絕對不會忽略異常或錯誤流。

但最重要的,你不能因爲這樣做調用CMD正確「CMD/C開始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(); 
     } 
    } 
} 
+1

非線程問題。 –

+0

@MK:正確,謝謝。 –

+0

這工作!謝謝這是一個線程問題 – user1058682

2

CMD開始將啓動一個新的命令提示窗口和您的輸入和輸出緩衝器將不被連接到它。