2012-07-03 183 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(); 
    } 

}