美文网首页mavenJava学习笔记我爱编程
Apache Commons Exec使用(运行外部程序)

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

作者: zlcook | 来源:发表于2017-06-06 18:15 被阅读254次
    <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");
    

    相关文章

      网友评论

        本文标题:Apache Commons Exec使用(运行外部程序)

        本文链接:https://www.haomeiwen.com/subject/reknfxtx.html