[关闭]
@tsing1226 2016-10-20T12:29:40.000000Z 字数 725 阅读 1084

java

java代码调用shell脚本


方法

  1. public static boolean exeShell(String command, String[] args) {
  2. String shellPath = "";
  3. shellPath = args[0];
  4. String cmd = "sh " + shellPath;
  5. Runtime run = Runtime.getRuntime();
  6. String result = "";
  7. BufferedReader br = null;
  8. BufferedInputStream in = null;
  9. try {
  10. Process p = run.exec("cmd /c start "+shellPath);
  11. if (p.waitFor() != 0) {
  12. result += "没有进程号";
  13. return false;
  14. }
  15. in = new BufferedInputStream(p.getInputStream());
  16. br = new BufferedReader(new InputStreamReader(in));
  17. String lineStr;
  18. while ((lineStr = br.readLine()) != null) {
  19. result += lineStr;
  20. }
  21. } catch (Exception e) {
  22. e.printStackTrace();
  23. return false;
  24. } finally {
  25. if (br != null) {
  26. try {
  27. br.close();
  28. in.close();
  29. } catch (IOException e) {
  30. e.printStackTrace();
  31. }
  32. }
  33. }
  34. return true;
  35. }

如果调用windows下的shell脚本,请安装git来执行shell脚本。git下载地址:https://git-scm.com/download/win.

添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注