4.在Linux下调用shell脚本并输出结果:
/**
* 在linux下调用shell脚本并在console端输出脚本的执行结果
* */
public class CallShell {
/**
* @throws Exception
*/
public static void main(String[] args) throws Exception {
String strCmd = "/home/zhu/test/test.sh";//待调用shell脚本
Process process = Runtime.getRuntime().exec(strCmd);//通过执行cmd命令调用protoc.exe程序
BufferedReader strCon = new BufferedReader(new InputStreamReader(process.getInputStream()));
String line;
while ((line = strCon.readLine()) != null) {
System.out.println("java print:"+line);
}
}
}
三、ProcessBuilder使用示例
ProcessBuilder的使用参考如下:
String[] as = new String[]{“待执行命令1”,"待执行命令2",.........};
ProcessBuilder pb = new ProcessBuilder(as);
pb.start();
使用ProcessBuilder可以依次执行多个命令