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();}}