import java.io.*;import java.lang.*;public class test4 { public static void main(String[] args) { try { Process p = Runtime.getRuntime().exec("ping www.google.com"); byte[] msg = new byte[128]; int len; while((len=p.getInputStream().read(msg)) > 0) { System.out.print(new String(msg, 0, len)); } byte[] rb = new byte[] { (byte)'\n' } ; OutputStream os = p.getOutputStream(); os.write(rb); os.close(); } catch (Exception e) { e.printStackTrace(); } }}
用java调cmd有很多例子的,可以根据返回值判断是否ping通。
1楼已经说了啊 获得进程返回的流 里面有进程返回的信息 如果返回Pinging www.l.google.com [66.249.89.147] with 32 bytes of data:Reply from 66.249.89.147: bytes=32 time=88ms TTL=243Reply from 66.249.89.147: bytes=32 time=87ms TTL=243Reply from 66.249.89.147: bytes=32 time=86ms TTL=243Reply from 66.249.89.147: bytes=32 time=89ms TTL=243Ping statistics for 66.249.89.147: Packets: Sent = 4, Received = 4, Lost = 0 (0% loss),Approximate round trip times in milli-seconds: Minimum = 86ms, Maximum = 89ms, Average = 87ms 这样的 信息 那就 成功了啊
import java.io.*;import java.lang.*;public class test4 { public static void main(String[] args) { try { Process p = Runtime.getRuntime().exec("ping www.google.com"); byte[] msg = new byte[128]; int len; while((len=p.getInputStream().read(msg)) > 0) { System.out.print(new String(msg, 0, len)); }
byte[] rb = new byte[] { (byte)'\n' } ; OutputStream os = p.getOutputStream(); os.write(rb); os.close(); } catch (Exception e) { e.printStackTrace(); } }}
获得进程返回的流 里面有进程返回的信息
如果返回Pinging www.l.google.com [66.249.89.147] with 32 bytes of data:Reply from 66.249.89.147: bytes=32 time=88ms TTL=243Reply from 66.249.89.147: bytes=32 time=87ms TTL=243Reply from 66.249.89.147: bytes=32 time=86ms TTL=243Reply from 66.249.89.147: bytes=32 time=89ms TTL=243Ping statistics for 66.249.89.147: Packets: Sent = 4, Received = 4, Lost = 0 (0% loss),Approximate round trip times in milli-seconds: Minimum = 86ms, Maximum = 89ms, Average = 87ms
这样的 信息 那就 成功了啊