Java中执行Linux命令     

```
/**

  1. * 执行Linux命令
  2. * @param commands 可执行多条命令
  3. * @throws Exception
  4. */
  5. public static void exec(List<String> commands) throws Exception {
  6. // 指定脚本执行目录为根目录,然后cd到程序的目录中执行shell脚本
  7. // 这样做是为了能够在进程中获得运行脚本的全路径,直接使用new File()来指定工作目录获取不到
  8. File wd = new File("/");
  9. Process process = null;
  10. process = Runtime.getRuntime().exec("/bin/bash", null, wd);// wd可选
  11. if (process != null) {
  12. BufferedReader in = new BufferedReader(new InputStreamReader(
  13. process.getInputStream()));
  14. PrintWriter

扫描下方二维码,加入Java方向技术交流讨论群。暗号:加群