-
一定要选对python编译器,因为可能脚本中含第三方库,而环境错误将会导致调用失败
-
当作以后调用python的一个小模板
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);
}
}
//调用python脚本函数
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("调用python脚本失败!");
} catch (InterruptedException e) {
e.printStackTrace();
}
return result;
}
}