Apache Commons Exec使用(运行外部程序)

<dependency>
    <groupId>org.apache.commons</groupId>
    <artifactId>commons-exec</artifactId>
    <version>1.3</version>
</dependency>

  • 作用:在java程序中开启进程执行外部程序。

  • 使用举例:

    • 1.在java程序中开启打印机程序打印文件。
    • 2.在java程序中执行外部脚本程序。
  • java支持:
    java提供了Runtime.exec() API来调用外部程序,Apache Commons Exec只是对其进行了封装。

  • 优点:(提供了对开启进程的管理)

  • 提供watchdog(看门狗)关闭挂起的进程。(比如打印机程序缺少纸,但是程序会处于阻塞状态无法结束)

  • 通过ExecuteResultHandler提供了异步执行外部程序的能力。默认是同步。

  • 使用更方便。

  • 参考案例:Java执行外部程序(Apache Commons Exec)

我的案例:

  • 在程序中执行本来通过run.bat脚本执行的内容。
    在run.bat中的内容:
spark-submit   --class "SimpleApp"   --master local[4]   D:/study/sparkstudy/simple-project/target/simple-project-1.0.jar
  • 通过程序完成run.bat脚本的内容
  • 第一种:
                    String line = "spark-submit.cmd   --class \"SimpleApp\"   --master local[4]  D:\\study\\sparkstudy\\simple-project\\target\\simple-project-1.0.jar";
                   
                     CommandLine cmdLine =   CommandLine.parse(line);
                     DefaultExecutor executor = new DefaultExecutor();
                     executor.setExitValues(null);
                     ExecuteWatchdog watchdog = new ExecuteWatchdog(60000);
                     executor.setWatchdog(watchdog);

                     ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
                     ByteArrayOutputStream errorStream = new ByteArrayOutputStream();
                     PumpStreamHandler streamHandler = new PumpStreamHandler(outputStream,errorStream);

                     executor.setStreamHandler(streamHandler);
                     executor.execute(cmdLine);
                     String out = outputStream.toString("gbk");//获取程序外部程序执行结果
                     String error = errorStream.toString("gbk");

第二种:

                     File file = new File("D:\\study\\sparkstudy\\simple-project\\target\\simple-project-1.0.jar");
                     Map map = new HashMap();
                     map.put("FILE", file);
                     CommandLine cmdLine =  new CommandLine("spark-submit.cmd");
                     cmdLine.addArgument("--class");
                     cmdLine.addArgument("\"SimpleApp\"");
                     cmdLine.addArgument("--master");
                     cmdLine.addArgument("local[4]");
                     cmdLine.addArgument("${FILE}");
                     cmdLine.setSubstitutionMap(map);

                     DefaultExecutor executor = new DefaultExecutor();
                     executor.setExitValues(null);
                     ExecuteWatchdog watchdog = new ExecuteWatchdog(60000);
                     executor.setWatchdog(watchdog);

                     ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
                     ByteArrayOutputStream errorStream = new ByteArrayOutputStream();
                     PumpStreamHandler streamHandler = new PumpStreamHandler(outputStream,errorStream);
                     executor.setStreamHandler(streamHandler);
                     executor.execute(cmdLine);
                     String out = outputStream.toString("gbk");//获取程序外部程序执行结果
                     String error = errorStream.toString("gbk");
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 134,837评论 18 139
  • 1. Java基础部分 基础部分的顺序:基本语法,类相关的语法,内部类的语法,继承相关的语法,异常的语法,线程的语...
    子非鱼_t_阅读 31,740评论 18 399
  • Spring Boot 参考指南 介绍 转载自:https://www.gitbook.com/book/qbgb...
    毛宇鹏阅读 46,925评论 6 342
  • 最近总感觉自己到了人生的低谷,这也许就是人到中年的危机与焦虑吧。上有老下有小,孩子的事、自己的事、家里的事...
    四眼蜗牛阅读 236评论 0 0
  • 点击文章标题即可阅读文章 1、吸霾八个月后,我回到食物森林 |【美国务农日记】(2) 2、【农场案例】关于一个家庭...
    青春实验室阅读 139评论 0 0