import org.python.util.PythonInterpreter;
import java.io.DataInputStream;
import java.util.Properties;
public class a {
public static void main(String[] args) throws Exception{
Properties props = new Properties();
props.put("python.home", "D:\\jython2.7.2\\software\\Lib");
props.put("python.console.encoding", "UTF-8");
props.put("python.security.respectJavaAccessibility", "false");
props.put("python.import.site", "false");
Properties properties = System.getProperties();
PythonInterpreter.initialize(properties, props, new String[0]);
PythonInterpreter interpreter = new PythonInterpreter();
interpreter.exec("print('hello')");
//当python文件中使用了 import 并调用了第三方库 执行以下命令就会报错
//interpreter.execfile("F:\\graduate\\pro\\src\\main\\java\\p1.py");
fun();
}
//此方法解决了python文件含有import 进入第三方库的问题
public static void fun() throws Exception{
String exe = "python";
String command = "F:\\graduate\\pro\\src\\main\\java\\p1.py";
String[] cmdArr = new String[] {exe,command};
Process process = Runtime.getRuntime().exec(cmdArr);
DataInputStream dis = new DataInputStream(process.getInputStream());
String str = "";
while((str = dis.readLine()) != null)
System.out.println(str);
}
}