java程序用调用linux命令执行,可以直接执行linux命令
传入动态目录
public void updateApprovalSh(String tempName) { try { // 执行脚本文件 String cmd = "mv " + filepathaudit +File.separator+ tempName+".xlsx" + " " + filepath; log.info("开始执行命令:" + cmd); //主要在这步写入后调用命令 Process process = Runtime.getRuntime().exec(cmd); try (PrintWriter printWriter = new PrintWriter( new BufferedWriter(new OutputStreamWriter(process.getOutputStream())), true); BufferedReader read = new BufferedReader(new InputStreamReader(process.getInputStream()))) { printWriter.println("cd " + filepath); printWriter.println(cmd); printWriter.println("exit"); String line; while ((line = read.readLine()) != null) { log.info(line); } } log.info( cmd + " 执行状态:" + process.waitFor() + " 执行时间:" + DateUtils.getStringDate2(new Date())); // Java父线程等待Shell子进程执行完毕 } catch (Exception e) { log.error("", e); throw new ShellRunException( "服务器运行Shell出错! " + DateUtils.getStringDate2(new Date())); } }
下一篇:
JVM中程序计数器的作用