java调用shell脚本,解决权限问题
2021-06-11 01:03
标签:chm code 路径 line tin pat roc ade exec java调用shell脚本,解决权限问题 标签:chm code 路径 line tin pat roc ade exec 原文地址:https://www.cnblogs.com/cchilei/p/14239115.html /**
* 解决了 参数中包含 空格和脚本没有执行权限的问题
* @param scriptPath 脚本路径
* @param para 参数数组
*/
private void execShell(String scriptPath) {
try {
//解决脚本没有执行权限
ProcessBuilder builder = new ProcessBuilder("/bin/chmod", "755",scriptPath);
Process process = builder.start();
process.waitFor();
Process ps = Runtime.getRuntime().exec(scriptPath);
ps.waitFor();
BufferedReader br = new BufferedReader(new InputStreamReader(ps.getInputStream()));
StringBuffer sb = new StringBuffer();
String line;
while ((line = br.readLine()) != null) {
sb.append(line).append("\n");
}
//执行结果
String result = sb.toString();
} catch (Exception e) {
e.printStackTrace();
}
}