您的位置:首页 > 运维架构 > Linux

JAVA利用Runtime执行多条linux命令

2015-06-08 21:30 302 查看
public static void execPythonShell(File file) throws IOException {

  String path = Utils.class.getClassLoader().getResource(“/import.py”)

    .getPath();

  Runtime runtime = Runtime.getRuntime();

  BufferedReader br = null;

  try {

   String[] cmds = new String[] {

     "/bin/sh",

     "-c",

     "cd " + path + "&&python edit_support_export_infor.py "

       + file.getPath() };

   Process process = runtime.exec(cmds);

   br = new BufferedReader(new InputStreamReader(

     process.getInputStream(), "GBK"));

   String tmp = null;

   while ((tmp = br.readLine()) != null) {

    System.out.println("Shell Message : " + tmp);

   }

  } catch (IOException e) {

   logger.error(e);

  } finally {

   br.close();

  }

 }

// -c 意思是执行完成自动关闭,这里多条linux命令通过&&连接到一起
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: