如果你的服务器上有 python 环境,那么有个非常方便的方案,什么都不用做。
ProcessBuilder processBuilder = new ProcessBuilder("/usr/bin/python3.11", "/path/to/
python.py");
try {
Process process = processBuilder.start();
try (BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream()))) {
String line;
while ((line = reader.readLine()) != null) {
// 你的逻辑
}
}
int exitCode = process.waitFor();
System.out.println("Exited with error code : " + exitCode);
} catch (IOException | InterruptedException e) {
System.err.println("An error occurred while executing the process: " + e.getMessage());
}