借助 Runtime.getRuntime().exec() 可以运行一个windows的exe程序
如图,使用java运行 ping 192.168.2.106,返回这样的字符串
如图,使用java运行 ping 192.168.2.106,返回这样的字符串
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
|
package socket; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; public class TestSocket { public static void main(String[] args) throws IOException { Process p = Runtime.getRuntime().exec( "ping " + "192.168.2.106" ); BufferedReader br = new BufferedReader( new InputStreamReader(p.getInputStream())); String line = null ; StringBuilder sb = new StringBuilder(); while ((line = br.readLine()) != null ) { if (line.length() != 0 ) sb.append(line + "
" ); } System.out.println( "本次指令返回的消息是:" ); System.out.println(sb.toString()); } } |