{"id":1120,"date":"2016-03-16T17:23:52","date_gmt":"2016-03-16T09:23:52","guid":{"rendered":"http:\/\/jingxianhome.aliapp.com\/?p=1120"},"modified":"2016-03-16T17:23:52","modified_gmt":"2016-03-16T09:23:52","slug":"apache-commons-exec%e6%95%99%e7%a8%8b","status":"publish","type":"post","link":"https:\/\/www.yeetrack.com\/?p=1120","title":{"rendered":"Apache Commons Exec\u6559\u7a0b"},"content":{"rendered":"<p>\u5982\u679c\u4f60\u770b\u5230\u8fd9\u7bc7\u6587\u7ae0\uff0c\u76f8\u4fe1\u4f60\u80af\u5b9a\u4f7f\u7528java\u65b0\u5efa\u8fc7\u5b50\u8fdb\u7a0b\uff0c\u53bb\u6267\u884cshell\u547d\u4ee4\uff0c\u5e76\u4e14\u80af\u5b9a\u8017\u8d39\u4e86\u4e0d\u5c11\u65f6\u95f4\u3002\u4f60\u53ef\u80fd\u4f1a\u8ba4\u4e3ajava\u81ea\u8eab\u7684<code>Runtime.exec()<\/code>\u5f88\u7b80\u5355\uff0c\u800cApache Commons Exec\u592a\u8fc7\u81c3\u80bf\uff0c\u7eaf\u7cb9\u662f\u5728\u6d6a\u8d39\u65f6\u95f4\u3002<\/p>\n<p>\u4f46\u662f\uff0c\u6211\u5728\u4f7f\u7528<code>Runtime.exec()<\/code>\u7684\u8fc7\u7a0b\u65f6\uff0c\u7ecf\u5386\u4e86\u4e00\u7cfb\u5217\u75db\u82e6\u7684\u8fc7\u7a0b\u3002\u4e00\u8d77\u6765\u770b\u4e0bcommons exec\u662f\u600e\u4e48\u628a\u8fd9\u4e00\u8fc7\u7a0b\u53d8\u7b80\u5355\u7684\u3002<!--more--><\/p>\n<h2>\u7b2c\u4e00\u4e2a\u4f8b\u5b50<\/h2>\n<p>\u4e0b\u9762\u662f\u4e00\u4e2a\u7b80\u5355\u7684\u4f8b\u5b50\uff0c\u6211\u4eec\u8c03\u7528\u4e86\u547d\u4ee4\u884c AcorRd32.exe\uff0c\u5f53\u7136\u524d\u63d0\u662f\u4f60\u7684\u7cfb\u7edf\u4e2d\u6709\u8fd9\u4e2a\u4e1c\u897f\u3002<\/p>\n<pre><code>    String line = \"AcroRd32.exe \/p \/h \" + file.getAbsolutePath();\n    CommandLine cmdLine = CommandLine.parse(line);\n    DefaultExecutor executor = new DefaultExecutor();\n    int exitValue = executor.execute(cmdLine);\n<\/code><\/pre>\n<p>\u4e0a\u9762\u7684\u4ee3\u7801\u6267\u884c\u540e\uff0c\u6211\u4eec\u6253\u5370\u51fa\u4e86pdf\u6587\u6863\u4e2d\u7684\u5185\u5bb9\uff0c\u4f46\u662f\u8fd0\u884c\u629b\u51fa\u5f02\u5e38\u4e86\u3002\u539f\u56e0\u662fAcrobat Reader\u8fd0\u884c\u7ed3\u675f\u540e\uff0c\u5b83\u7684\u8fd4\u56de\u503c\u662f1\uff0c\u800c\u4e00\u822c\u60c5\u51b5\u4e0b\u8ba4\u4e3a\u8fd4\u56de\u503c\u4e3a1\u662f\u9519\u8bef\u7684\uff0c\u6240\u4ee5\u6211\u4eec\u9700\u8981\u4fee\u6539\u4e00\u4e0b\u4ee3\u7801\u3002<\/p>\n<pre><code>    String line = \"AcroRd32.exe \/p \/h \" + file.getAbsolutePath();\n    CommandLine cmdLine = CommandLine.parse(line);\n    DefaultExecutor executor = new DefaultExecutor();\n    executor.setExitValue(1);\n    int exitValue = executor.execute(cmdLine);\n<\/code><\/pre>\n<h2>\u662f\u5426\u4f7f\u7528Watchdog<\/h2>\n<p>\u6211\u4eec\u7684\u7a0b\u5e8f\u8f93\u51fa\u4e86pdf\u5185\u5bb9\u3002\u4f46\u662f\u5982\u679c\u5728\u8f93\u51fa\u8fc7\u7a0b\u4e2d\uff0c\u6211\u4eec\u7684\u7a0b\u5e8f\u56e0\u4e3a\u5404\u79cd\u539f\u56e0hang\u4f4f\u4e86\uff0c\u600e\u4e48\u529e\uff1f\u542f\u52a8\u4efb\u52a1\u5f88\u7b80\u5355\uff0c\u4f46\u662f\u5982\u679c\u5b50\u8fdb\u7a0b\u5931\u8d25\u4e86\u600e\u4e48\u529e\u3002Commons-exec\u63d0\u4f9b\u4e86watchdog\u6765\u89e3\u51b3\u8fd9\u4e00\u95ee\u9898\u3002\u4e0b\u9762\u7684\u4ee3\u7801\u4e2d\uff0c\u5982\u679c\u5b50\u8fdb\u7a0b\u8fd0\u884c\u8d85\u8fc76\u79d2\uff0c\u90a3\u4e48\u5c31\u5f3a\u5236\u7ed3\u675f\u5b83\u3002<\/p>\n<pre><code>    String line = \"AcroRd32.exe \/p \/h \" + file.getAbsolutePath();\n    CommandLine cmdLine = CommandLine.parse(line);\n    DefaultExecutor executor = new DefaultExecutor();\n    executor.setExitValue(1);\n    ExecuteWatchdog watchdog = new ExecuteWatchdog(60000);\n    executor.setWatchdog(watchdog);\n    int exitValue = executor.execute(cmdLine);\n<\/code><\/pre>\n<h2>\u5f15\u53f7\u5f88\u6709\u7528<\/h2>\n<p>\u4e0a\u9762\u7684\u4ee3\u7801\u8fd8\u6709\u4e2a\u95ee\u9898\uff0c\u5982\u679c\u8f93\u5165\u7684\u6587\u4ef6\u8def\u5f84\u4e2d\u542b\u6709\u7a7a\u683c\uff0c\u4f1a\u6709\u5f02\u5e38\u3002<\/p>\n<pre><code>    &gt; AcroRd32.exe \/p \/h C:\\Document And Settings\\documents\\432432.pdf  \n<\/code><\/pre>\n<p>\u89e3\u51b3\u65b9\u6cd5\u662f\uff0c\u4f7f\u7528\u5f15\u53f7\u5c06\u6587\u4ef6\u8def\u5f84\u62ec\u8d77\u6765\uff1a<\/p>\n<pre><code>    String line = \"AcroRd32.exe \/p \/h \\\"\" + file.getAbsolutePath() + \"\\\"\";\n    CommandLine cmdLine = CommandLine.parse(line);\n    DefaultExecutor executor = new DefaultExecutor();\n    executor.setExitValue(1);\n    ExecuteWatchdog watchdog = new ExecuteWatchdog(60000);\n    executor.setWatchdog(watchdog);\n    int exitValue = executor.execute(cmdLine);\n<\/code><\/pre>\n<h2>\u9010\u6b65\u751f\u6210CommandLine\u5bf9\u8c61<\/h2>\n<p>\u4e0a\u9762\u7684\u5f15\u53f7\u95ee\u9898\uff0c\u662f\u56e0\u4e3acommons-exec\u9519\u8bef\u5730\u5206\u5272\u4e86\u6211\u4eec\u7684\u8def\u5f84\u5b57\u7b26\u4e32\uff0c\u5bfc\u81f4\u6587\u4ef6\u8def\u5f84\u9519\u8bef\uff0c\u6211\u4eec\u53ef\u662f\u4f7f\u7528CommandLine\u5bf9\u8c61\u63d0\u4f9b\u7684\u65b9\u6cd5\uff0c\u6765\u907f\u514d\u624b\u52a8\u62fc\u88c5\u547d\u4ee4\u3002<\/p>\n<pre><code>    Map map = new HashMap();\n    map.put(\"file\", new File(\"invoice.pdf\"));\n    CommandLine cmdLine = new CommandLine(\"AcroRd32.exe\");\n    cmdLine.addArgument(\"\/p\");\n    cmdLine.addArgument(\"\/h\");\n    cmdLine.addArgument(\"${file}\");\n    cmdLine.setSubstitutionMap(map);\n    DefaultExecutor executor = new DefaultExecutor();\n    executor.setExitValue(1);\n    ExecuteWatchdog watchdog = new ExecuteWatchdog(60000);\n    executor.setWatchdog(watchdog);\n    int exitValue = executor.execute(cmdLine);\n<\/code><\/pre>\n<h2>\u5f02\u6b65\u6267\u884c<\/h2>\n<p>\u73b0\u5728\u4e3a\u6b62\uff0c\u6211\u4eec\u7684\u7a0b\u5e8f\u53ef\u4ee5\u826f\u597d\u8fd0\u884c\u4e86\uff0c\u4f46\u662f\u8fd8\u4e0d\u9002\u7528\u751f\u4ea7\uff0c\u539f\u56e0\u662f\u5b83\u662f\u963b\u585e\u7684\uff0c\u5728\u5b50\u8fdb\u7a0b\u7ed3\u675f\u524d\uff0c\u5b83\u4f1a\u4e00\u76f4\u963b\u585e\u6211\u4eec\u7684\u4e3b\u8fdb\u7a0b\u3002\u73b0\u5728\u6211\u4eec\u8981\u628a\u5b83\u53d8\u6210\u975e\u963b\u585e\u7684\u3002\u4e0b\u9762\u7684\u4ee3\u7801\uff0c\u6211\u4eec\u521b\u5efa\u4e86\u4e00\u4e2a<strong>ExecuteResultHandler<\/strong>\u5bf9\u8c61\uff0c\u5c06\u5b83\u4f20\u7ed9<strong>Executor<\/strong>\uff0c\u8ba9\u5b83\u53ef\u4ee5\u5f02\u6b65\u6267\u884c\u4efb\u52a1\u3002<strong>ResultHandler<\/strong>\u4f1a\u63a5\u53d7\u5b50\u8fdb\u7a0b\u629b\u51fa\u7684\u4e00\u5207\u5f02\u5e38\u3002<\/p>\n<pre><code>    CommandLine cmdLine = new CommandLine(\"AcroRd32.exe\");\n    cmdLine.addArgument(\"\/p\");\n    cmdLine.addArgument(\"\/h\");\n    cmdLine.addArgument(\"${file}\");\n    HashMap map = new HashMap();\n    map.put(\"file\", new File(\"invoice.pdf\"));\n    commandLine.setSubstitutionMap(map);\n\n    DefaultExecuteResultHandler resultHandler = new         DefaultExecuteResultHandler();\n\n    ExecuteWatchdog watchdog = new ExecuteWatchdog(60*1000);\n    Executor executor = new DefaultExecutor();\n    executor.setExitValue(1);\n    executor.setWatchdog(watchdog);\n    executor.execute(cmdLine, resultHandler);\n\n    \/\/\u4e00\u6bb5\u65f6\u95f4\u540e\uff0cresultHandler\u7684\u56de\u8c03\u65b9\u6cd5\u4f1a\u88ab\u5524\u9192\uff0c\u6211\u4eec\u53ef\u4ee5\u67e5\u770b\u5176\u8fd4\u56de\u503c\n    int exitValue = resultHandler.waitFor();\n<\/code><\/pre>\n<p>\u672c\u6559\u7a0b\u4e2d\u4e2d\u7684\u6253\u5370\u4efb\u52a1\uff0c\u4e00\u4efd\u5b8c\u6574\u7684\u4ee3\u7801\u53c2\u8003\u6b64\u5904\uff0c<a href=\"http:\/\/commons.apache.org\/proper\/commons-exec\/xref-test\/org\/apache\/commons\/exec\/TutorialTest.html\">http:\/\/commons.apache.org\/proper\/commons-exec\/xref-test\/org\/apache\/commons\/exec\/TutorialTest.html<\/a><\/p>\n<h2>\u83b7\u53d6\u7a0b\u5e8f\u8f93\u51fa\u7684\u4f8b\u5b50<\/h2>\n<pre><code>    package com.yeetrack.weixinalert;\n\n    import org.apache.commons.exec.CommandLine;\n    import org.apache.commons.exec.DefaultExecutor;\n    import org.apache.commons.exec.PumpStreamHandler;\n\n    import java.io.ByteArrayOutputStream;\n    import java.io.IOException;\n\n    \/**\n    * Created by Xuemeng Wang on 15\/4\/2.\n    *\/\n    public class ShellUtil {\n        DefaultExecutor defaultExecutor;\n        ByteArrayOutputStream outputStream;\n        ByteArrayOutputStream errorStream;\n        PumpStreamHandler pumpStreamHandler;\n\n        public ShellUtil()\n        {\n            defaultExecutor = new DefaultExecutor();\n            outputStream = new ByteArrayOutputStream();\n            errorStream = new ByteArrayOutputStream();\n            pumpStreamHandler = new PumpStreamHandler(outputStream, errorStream);\n            defaultExecutor.setStreamHandler(pumpStreamHandler);\n        }\n\n        public void execute(CommandLine commandLine)\n        {\n            try {\n                defaultExecutor.execute(commandLine);\n            } catch (IOException e) {\n                e.printStackTrace();\n            }\n        }\n\n        public String getOutAsString()\n        {\n            return outputStream.toString();\n        }\n        public String getErrorAsString()\n        {\n            return errorStream.toString();\n        }\n    }\n<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u5982\u679c\u4f60\u770b\u5230\u8fd9\u7bc7\u6587\u7ae0\uff0c\u76f8\u4fe1\u4f60\u80af\u5b9a\u4f7f\u7528java\u65b0\u5efa\u8fc7\u5b50\u8fdb\u7a0b\uff0c\u53bb\u6267\u884cshell\u547d\u4ee4\uff0c\u5e76\u4e14\u80af\u5b9a\u8017\u8d39\u4e86\u4e0d\u5c11\u65f6\u95f4\u3002\u4f60\u53ef\u80fd\u4f1a\u8ba4\u4e3ajava\u81ea\u8eab\u7684Runtime.exec()\u5f88\u7b80\u5355\uff0c\u800cApache Commons Ex&#46;&#46;&#46;<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"pgc_sgb_lightbox_settings":"","footnotes":""},"categories":[33],"tags":[8,11,59],"class_list":["post-1120","post","type-post","status-publish","format-standard","hentry","category-coding","tag-java","tag-linux","tag-shell"],"views":7728,"_links":{"self":[{"href":"https:\/\/www.yeetrack.com\/index.php?rest_route=\/wp\/v2\/posts\/1120","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.yeetrack.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.yeetrack.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.yeetrack.com\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.yeetrack.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1120"}],"version-history":[{"count":2,"href":"https:\/\/www.yeetrack.com\/index.php?rest_route=\/wp\/v2\/posts\/1120\/revisions"}],"predecessor-version":[{"id":1122,"href":"https:\/\/www.yeetrack.com\/index.php?rest_route=\/wp\/v2\/posts\/1120\/revisions\/1122"}],"wp:attachment":[{"href":"https:\/\/www.yeetrack.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1120"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.yeetrack.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1120"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.yeetrack.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1120"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}