1 需要把shell脚本放在resources目录下。如shell.sh
2需要在tomcat里面定位到当前shell的目录,不要定位到了tomcat的目录里面去
定位代码
String path1 = Thread.currentThread().getContextClassLoader().getResource("").getPath();//获取当前资源的虚拟路径
3 完善shell执行命令
String cmd = "bash " +path1 + "shell.sh";
4 执行shell
Runtime rt = Runtime.getRuntime();
Process proc = rt.exec(cmd, null, null);
额外小计
通过Process proc对象可以获得后续的输出流。异常流等 proc.getInputStream(); proc.getErrorStream();