来源:http://www.bug315.com/article/109.htmimport java.io.*;
/**
* Java调用windows的DOS命令
* 实现调用Windows的ipconfig命令,然后将输出的信息通过IO流输出到控制台。
*/
public class RunWindowsCommand{
public static void main(String[] args) {
InputStream ins = null;
String[] cmd = new String[] { "cmd.exe", "/C", "ipconfig" }; // 命令
try {
Process process = Runtime.getRuntime().exec(cmd);
ins = process.getInputStream(); // 获取执行cmd命令后的信息
BufferedReader reader = new BufferedReader(new InputStreamReader(ins));
String line = null;
while ((line = reader.readLine()) != null) {
System.out.println(line); // 输出
}
int exitValue = process.waitFor();
System.out.println("返回值:" + exitValue);
process.getOutputStream().close(); // 不要忘记了一定要关
} catch (Exception e) {
e.printStackTrace();
}
}
}
来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/26875129/viewspace-1434264/,如需转载,请注明出处,否则将追究法律责任。