LMY

IOS Android C# Python Silverlight Flex HTML5 WCF GIS .NET.....现已博客已移至http://blog.512z.com

  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

由于在项目需要执行Python,找寻相关资料,总结出以下几种方式:

  1. 直接执行Python脚本代码

    引用 org.python包

1 PythonInterpreter interpreter = new PythonInterpreter();  
2 interpreter.exec("days=('mod','Tue','Wed','Thu','Fri','Sat','Sun'); ");   ///执行python脚本

  

  2.  执行python .py文件

1 PythonInterpreter interpreter = new PythonInterpreter();  
2 InputStream filepy = new FileInputStream("D:\\demo.py"); 
3 interpreter.execfile(filepy);  ///执行python py文件
4 filepy.close();

  

  3.  使用Runtime.getRuntime()执行脚本文件

     这种方式和.net下面调用cmd执行命令的方式类似。如果执行的python脚本有引用第三方包的,建议使用此种方式。使用上面两种方式会报错java ImportError: No module named arcpy。

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

 

posted on 2013-10-11 14:45  李明杨  阅读(43852)  评论(1编辑  收藏  举报