java调用系统命令Demo

java调用系统命令范例

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;

public class SystemCommandCall {
    public static void main(String[] args) throws Exception {
        String cmd = "ping 8.8.8.8"; //运行的命令

        Runtime runtime = Runtime.getRuntime();

        Process p = runtime.exec(cmd);

        InputStream is = p.getInputStream();
        InputStreamReader isr = new InputStreamReader(is, "UTF-8");
        BufferedReader reader = new BufferedReader(isr);

        for (String line; (line = reader.readLine()) != null;) {
            System.out.println(line);
        }
    }
}

发表评论

电子邮件地址不会被公开。 必填项已用*标注