Be sure to choose the right one python compiler , Because the script may contain a third-party library , An environment error will cause the call to fail
As if to call later python A small template for
package com.neusoft;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.BufferedReader;
public class demo {
public static void main(String[] args) {
try {
String res = getRes();
System.out.println(res);
} catch (Exception e) {
System.out.println(e);
}
}
// call python Script functions
public static String getRes() {
String result = "";
try {
Process process;
process = Runtime.getRuntime().exec("D:/mysoft/anaconda/conda/envs/tf2py37/python.exe E:/javademos/serverlet1/Python/animefaces.py");
BufferedReader ir = new BufferedReader(new InputStreamReader(process.getInputStream()));
String line = null;
while ((line = ir.readLine()) != null) {
result = line;
}
ir.close();
process.waitFor();
} catch (IOException e) {
System.out.println(" call python Script failed !");
} catch (InterruptedException e) {
e.printStackTrace();
}
return result;
}
}