linux环境下java读取sh脚本并执行
Process process;
String cmd = "/home/ty/t.sh";//这里必须要给文件赋权限 chmod u+x fileName;
try {
// 使用Runtime来执行command,生成Process对象
Runtime runtime = Runtime.getRuntime();
process = runtime.exec(cmd);
// 取得命令结果的输出流
InputStream is = process.getInputStream();
// 用一个读输出流类去读
InputStreamReader isr = new InputStreamReader(is);
// 用缓冲器读行
BufferedReader br = new BufferedReader(isr);
String line = null;
while ((line = br.readLine()) != null) {
System.out.println(line);
}
//执行关闭操作
is.close();
isr.close();
br.close();
} catch (IOException e) {
e.printStackTrace();
}