直接看源程序吧
public static void main(String[] args) throws IOException {
File dir = new File("D:\\");
// String command="netstat -an";
<span style="color: #ff0000;">String command = "c:\\windows\\system32\\cmd.exe /c netstat -an";
Runtime r = Runtime.getRuntime();
Process p = r.exec(command, null, dir);
</span> BufferedReader br = new BufferedReader(new InputStreamReader(p
.getInputStream()));
StringBuffer sb = new StringBuffer();
String inline;
while (null != (inline = br.readLine())) {
sb.append(inline).append("\n");
}
System.out.println(sb.toString());
}
最重要的是exec方法中的几个参数,可以动态的旨定执行的目录,这样如果想执行对应的命令那么就只需要指定对应的目录就可以了。