1200字范文,内容丰富有趣,写作的好帮手!
1200字范文 > Java调用python打包的程序.exe 包括获取exec()中打印的日志 亲测有效

Java调用python打包的程序.exe 包括获取exec()中打印的日志 亲测有效

时间:2022-12-15 14:28:44

相关推荐

Java调用python打包的程序.exe 包括获取exec()中打印的日志 亲测有效

python写了一个方法,windows平台上怎么被Java服务调用呢?

最简单的办法,python利用pyinstaller打包成.exe程序,Java程序通过Process调用,想同时获取.exe执行过程中打印出的日志用ProcessBuilder

亲测有效,源代码如下

import java.io.BufferedReader;import java.io.IOException;import java.io.InputStreamReader;public class TestOutput {public static void main(String[] arguments) throws IOException, InterruptedException {System.out.println(getProcessOutput());}public static String getProcessOutput() throws IOException, InterruptedException {// 参数1 exe的绝对路径 参数2 -i 参数3 las文件目录 23执行exe程序必须的参数ProcessBuilder processBuilder = new ProcessBuilder("D:\\lazConvertor.exe","-i","F:\\las_test\\");processBuilder.redirectErrorStream(true);Process process = processBuilder.start();System.out.println("start: " + process.isAlive());StringBuilder processOutput = new StringBuilder();try (BufferedReader processOutputReader = new BufferedReader(new InputStreamReader(process.getInputStream()));) {String readLine;while ((readLine = processOutputReader.readLine()) != null) {processOutput.append(readLine + System.lineSeparator());}process.waitFor();} catch (IOException e) {System.out.println(e.getMessage());} catch (InterruptedException e) {System.out.println(e.getMessage());} finally {if (process != null) {process.destroy();}}return processOutput.toString().trim();}}

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。