2012-07-03 161 views
-4

我同時使用此代碼代碼未運行

Runtime rt = Runtime.getRuntime(); 
System.out.println("Executing " + "uname -a"); 
Process x = rt.exec("ping IP Address of system"); 
BufferedReader stdInput = 
    new BufferedReader(new InputStreamReader(x.getInputStream())); 
BufferedReader stdError = 
    new BufferedReader(new InputStreamReader(x.getErrorStream())); 
String line = null; 
while ((line = stdInput.readLine()) != null) { 
    System.out.println(line); 
} 
x.waitFor();   
+4

什麼例外,你得到準確?你對此有何疑問? – Mat

+0

你可以粘貼異常堆棧跟蹤嗎? – Florent

+1

我在Windows 7 64bit上試過了,它工作得很好。 –

回答

0

嘗試這種方式得到一個運行時異常..

public class PingClass { 

    public void pingIt(){ 

     InetAddress addr; 
     try { 
      String line; 

      Process p = Runtime.getRuntime().exec(
        "C:/windows/system32/ping.exe 192.168.2.2"); 

      /** 
      * Create a buffered reader from the Process' input stream. 
      */ 
      BufferedReader input = new BufferedReader(new InputStreamReader(p 
        .getInputStream())); 

      /** 
      * Loop through the input stream to print the program output into console. 
      */ 
      ArrayList<String> str = new ArrayList<String>(); 
      while ((line = input.readLine()) != null) { 
       str.add(line); 
       System.out.println(line); 

      } 
      /** 
      * Finally close the reader 
      */ 
      input.close(); 
     } catch (Exception e) { 
      e.printStackTrace(); 
     } 
    } 


    public static void main(String[] args){ 


     new PingClass().pingIt(); 
    } 

}