Java 调用windows .exe

    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();
            }
        }
    }
}
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。