牛骨文教育服务平台(让学习变的简单)
博文笔记

Java之——运行Python脚本的几种方式

创建时间:2016-03-17 投稿人: 浏览次数:11081

转载请注明出处:http://blog.csdn.net/l1028386804/article/details/50914261

1、直接执行Python脚本代码

 引用 org.python包

PythonInterpreter interpreter = new PythonInterpreter();  
interpreter.exec("numbers=("1","2","3","4","5","6","7"); ");   ///执行python脚本
2、执行python .py文件
PythonInterpreter interpreter = new PythonInterpreter();  
InputStream filepy = new FileInputStream("D:\execute_command.py"); 
interpreter.execfile(filepy);  ///执行python py文件
filepy.close();
3、使用Runtime.getRuntime()执行脚本文件

这种方式和.net下面调用cmd执行命令的方式类似。如果执行的python脚本有引用第三方包的。

注意:建议使用此种方式。使用上面两种方式会报错java ImportError: No module named arcpy。

Process proc = Runtime.getRuntime().exec("python  D:\execute_command.py");  
proc.waitFor(); 

声明:该文观点仅代表作者本人,牛骨文系教育信息发布平台,牛骨文仅提供信息存储空间服务。