//调用shell脚本
public String callCMD1(String message) {
Process process;
InputStreamReader in = null;
LineNumberReader input = null;
try {
process = Runtime.getRuntime().exec(message);
in = new InputStreamReader(process.getInputStream());
input = new LineNumberReader(in);
String line = "";
while ((line = input.readLine()) != null) {
System.out.println(line);
}
return line;
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (in != null) {
in.close();
}
if (input != null) {
input.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
return null;
}
//调用命令
// int cmd=callCMD("rm -rf /usr/local/*"); 0成功
public int callCMD(String message) throws Exception {
String[] cmd = {"/bin/sh", "-c", message};
Process process = Runtime.getRuntime().exec(cmd);
int waitFor = process.waitFor();
return waitFor;
}
java调用shell脚本和命令
最新推荐文章于 2024-04-27 12:17:02 发布