-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

1 回答 1

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();
    }

}
于 2012-07-03T09:29:43.137 回答