Chinaunix首页 | 论坛 | 博客
  • 博客访问: 3968659
  • 博文数量: 366
  • 博客积分: 9916
  • 博客等级: 中将
  • 技术积分: 7195
  • 用 户 组: 普通用户
  • 注册时间: 2011-05-29 23:27
个人简介

简单!

文章分类

全部博文(366)

文章存档

2013年(51)

2012年(269)

2011年(46)

分类: Java

2012-11-12 17:08:12


  1. /**
  2.  * 运行shell脚本
  3.  * @param shell 需要运行的shell脚本
  4. */
  5. public static void execShell(String shell) {
  6.     try {
  7.         Runtime rt = Runtime.getRuntime();
  8.         rt.exec(shell);
  9.     } catch (Exception e) {
  10.         e.printStackTrace();
  11.     }
  12. }


  13. /**
  14.  * 运行shell
  15.  *
  16.  * @param shStr
  17.  * 需要执行的shell
  18.  * @return
  19.  * @throws IOException
  20.  */
  21. public static List runShell(String shStr) throws Exception {
  22.     List<String> strList = new ArrayList();

  23.     Process process;
  24.     process = Runtime.getRuntime().exec(new String[] {"/bin/sh","-c",shStr},null,null);
  25.     InputStreamReader ir = new InputStreamReader(process
  26.             .getInputStream());
  27.     LineNumberReader input = new LineNumberReader(ir);
  28.     String line;
  29.     process.waitFor();
  30.     while ((line = input.readLine()) != null) {
  31.         strList.add(line);
  32.     }

  33.     return strList;
  34. }
注:如果sh中含有awk,一定要按new String[]{"/bin/sh","-c",shStr}写,才可以获得流.
阅读(6797) | 评论(0) | 转发(3) |
给主人留下些什么吧!~~