Java调用python打包的程序.exe,包括获取exec()中打印的日志,亲测有效
生活随笔
收集整理的這篇文章主要介紹了
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();}
}
總結
以上是生活随笔為你收集整理的Java调用python打包的程序.exe,包括获取exec()中打印的日志,亲测有效的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: centos7.4.3 部署python
- 下一篇: 使用Python,OpenCV加载图像并