美文网首页Java web
Java 调用windows .exe

Java 调用windows .exe

作者: 咖啡机an | 来源:发表于2019-01-25 16:47 被阅读0次
        public static void runExe() throws IOException {
            Process process = null;
            try {
                process = Runtime.getRuntime().exec(exePath);
                //防止程序阻塞,需要把缓存中的内容消费掉
                taskExecutor.execute(new InputStreamRunnable(process.getErrorStream(), filePath, 1));
                taskExecutor.execute(new InputStreamRunnable(process.getInputStream(), null, 1));
                process.waitFor();
                LOGGER.info(url);
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                if (process != null) {
                    process.destroy();
                }
            }
        }
    
    public class InputStreamRunnable implements Runnable {
        private static final Logger LOGGER = LoggerFactory.getLogger(InputStreamRunnable.class);
        private BufferedReader bReader = null;
        private String fileUrl = null;
        private Integer logOut = null;
        public InputStreamRunnable(InputStream is, String url, Integer logOut) {
            try {
                bReader = new BufferedReader(new InputStreamReader(new BufferedInputStream(is), "GBK"));
                fileUrl = url;
                this.logOut = logOut;
            } catch (UnsupportedEncodingException e) {
                e.printStackTrace();
            }
        }
    
        @Override
        public void run() {
            StringBuffer line = new StringBuffer();
            try {
                String str = "";
                while ((str = bReader.readLine()) != null) {
                    line.append(str).append(System.lineSeparator());
                }
                if (logOut != null && logOut == 1) {
                    LOGGER.info(line.toString());
                    if (!StringUtils.isEmpty(fileUrl)) {
                        LOGGER.info( line.toString());
                    }
                }
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                try {
                    if (bReader != null) {
                        bReader.close();
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
    

    相关文章

      网友评论

        本文标题:Java 调用windows .exe

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