如何在JAVA环境中运行命令行?

问答中心分类: JAVA如何在JAVA环境中运行命令行?
jboeyin asked 7 年 ago

我的服务器安装了ONEINSTACK 的 JAVA环境,程序中调用了命令行 PING 域名,为什么没有返回任何结果,是否遗忘了什么?
private void exeCmd(String commandStr) {
BufferedReader br = null;
try {
Process p = Runtime.getRuntime().exec(commandStr);
br = new BufferedReader(new InputStreamReader(p.getInputStream(),Charset.forName("GBK")));
String line = null;
StringBuilder sb = new StringBuilder();
while ((line = br.readLine()) != null) {
sb.append(line + "\n");
}
System.out.println("COMMAND LINE RESULT: "+sb.toString());
} catch (Exception e) {
e.printStackTrace();
} finally {
if (br != null){
try {
br.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
}

Question Tags:

1 Answers
oneinstack answered 7 年 ago
是否加次数限制?