- /**
- * 与外部进程通信
- * 输入参数 127.0.0.1
- * */
- import java.io.*;
- public class Ping {
- public static void main(String[] args) {
- Process process = null;
- try {
- process = Runtime.getRuntime().exec("ping " + args[0]);//Runtime类exec()方法执行外部程序
- InputStream is = process.getInputStream();//获取子进程的输入流
- BufferedReader br = new BufferedReader(new InputStreamReader(is));
- String s = null;
- while ((s = br.readLine()) != null) {
- System.out.println(s);
- }
- } catch (IOException ex) {
- ex.printStackTrace(System.out);
- System.err.println("创建进程时出错...");
- System.exit(1);//返回子进程的出口值
- } finally {
- process.destroy();
- }
- }
- }
JDK6.0学习笔记(二十三)与外部进程通信
最新推荐文章于 2023-10-30 19:30:56 发布