public static String exeCmd(String commandStr) {
BufferedReader br = null;
Process process=null;
try {
process = Runtime.getRuntime().exec(commandStr);
br = new BufferedReader(new InputStreamReader(process.getInputStream(), Charset.forName("GBK")));
String line = null;
StringBuilder sb = new StringBuilder();
while ((line = br.readLine()) != null) {
sb.append(line + "\n");
}
System.out.println(sb.toString());
return sb.toString();
} catch (Exception e) {
if (br != null){
try {
br.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
public static void main(String[] args) {
String commandStr = "ping 10.37.0.56";
exeCmd(commandStr);
BufferedReader br = null;
Process process=null;
try {
process = Runtime.getRuntime().exec(commandStr);
br = new BufferedReader(new InputStreamReader(process.getInputStream(), Charset.forName("GBK")));
String line = null;
StringBuilder sb = new StringBuilder();
while ((line = br.readLine()) != null) {
sb.append(line + "\n");
}
System.out.println(sb.toString());
return sb.toString();
} catch (Exception e) {
e.printStackTrace();
return "";
if (br != null){
try {
br.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
public static void main(String[] args) {
String commandStr = "ping 10.37.0.56";
exeCmd(commandStr);
}
Charset.forName("GBK")可以防止返回结果乱码