win和linux下执行cmd或者sh


linux:

InputStreamReader isr = null;
BufferedReader br = null;
ProcessBuilder pb = new ProcessBuilder("sh", "-c", cmd.toString());
pb.redirectErrorStream(true);
Process ps = pb.start();

isr = new InputStreamReader(ps.getInputStream());
br = new BufferedReader(isr);
String line = "";
while ((line = br.readLine()) != null) {
}

win:


InputStreamReader isr = null;
BufferedReader br = null;
Process ps = Runtime.getRuntime().exec("cmd.exe /c "+cmd.toString()+"");
isr = new InputStreamReader(ps.getInputStream());
br = new BufferedReader(isr);
String line = "";
while ((line = br.readLine()) != null) {
}

你可能感兴趣的:(java win cmd)