Java 执行脚本
之前做需求需要用java执行压缩(“tar -czvf ***”)并下载文件,于是用java代码直接执行tar命令,最后压缩文件里面是空的,这个问题挂了好几天
今天换个办法解决了
把脚本卸载服务器上 然后执行这个脚本
/** * 运行shell * * @param shStr * 需要执行的shell命令 * @return 回显 * @throws IOException */public static List<String> runShell(String shStr) throws Exception {List<String> strList = new ArrayList<String>();Process process;process = Runtime.getRuntime().exec(new String[] { "/bin/sh", "-c", shStr }, null, null);// process = Runtime.getRuntime().exec(shStr);InputStreamReader ir = new InputStreamReader(process.getInputStream());LineNumberReader input = new LineNumberReader(ir);String line;process.waitFor();while ((line = input.readLine()) != null) {System.out.println(line);strList.add(line);}return strList;}