public class Test {
public static void cmd(String cmdLocation) {
try {
//cmdLocation 是要执行dos命令的路径 下面的.exec 相当于windows命令的win+R后+CMD
Process pro = Runtime.getRuntime().exec(Cmdlocation);
//也可以这样写命令
//String[] cmd = new String[] { "cmd.exe", "/C", "ipconfig" };
//Process pro = Runtime.getRuntime().exec(cmd);
BufferedReader br = new BufferedReader(new InputStreamReader(pro.getInputStream()));
StringBuffer strbr = new StringBuffer();
String line;
// 获取命令执行的结果
while ((line = br.readLine())!= null) {
//这里可以做一些操作
}
try {
pro.waitFor();
// 这里看情况关不关,比如跑jar包 就不用关,跑一次性的就关
pro.getOutputStream().close();
} catch (InterruptedException e) {
e.printStackTrace();
}
} catch (IOException e) {
e.printStackTrace();
}
}