Java程序中实现调用Python脚本的方法详解
本文实例讲述了java程序中实现调用python脚本的方法。分享给大家供大家参考,具体如下:
在程序开发中,有时候需要java程序中调用相关python脚本,以下内容记录了先关步骤和可能出现问题的解决办法。
1、在eclipse中新建maven工程;
2、pom.xml文件中添加如下依赖包之后update maven工程;
<dependency> <groupid>org.python</groupid> <artifactid>jython</artifactid> <version>2.7.0</version> </dependency> <dependency> <groupid>org.python</groupid> <artifactid>jython-standalone</artifactid> <version>2.7.0</version> </dependency>
3、编写如下测试代码;
import org.python.util.pythoninterpreter; public class jpythonscript { public static void main(string args[]) { pythoninterpreter interpreter = new pythoninterpreter(); interpreter.exec("days=('mod','tue','wed','thu','fri','sat','sun'); "); interpreter.exec("print days[1];"); } }
4、测试:
出现如下错误:
console: failed to install '': java.nio.charset.unsupportedcharsetexception: cp0.
exception in thread "main" importerror: cannot import site module and its dependencies: no module named site
determine if the following attributes are correct:
* sys.path: ['...python\\jython\\2.7.0\\lib', '__classpath__', '__pyclasspath__/']
this attribute might be including the wrong directories, such as from cpython
* sys.prefix:***\jython\2.7.0
this attribute is set by the system property python.home, although it can
be often automatically determined by the location of the jython jar file
you can use the -s option or python.import.site=false to not import the site module
5、问题解决:
重构代码如下:
import java.util.properties; import org.python.util.pythoninterpreter; public class jpythonscript { public static void main(string args[]) { properties props = new properties(); props.put("python.home", "path to the lib folder"); props.put("python.console.encoding", "utf-8"); props.put("python.security.respectjavaaccessibility", "false"); props.put("python.import.site", "false"); properties preprops = system.getproperties(); pythoninterpreter.initialize(preprops, props, new string[0]); pythoninterpreter interpreter = new pythoninterpreter(); interpreter.exec("days=('mod','tue','wed','thu','fri','sat','sun'); "); interpreter.exec("print days[1];"); } }
6、编译成功。
7、解决问题参考:
补充:jpython抛错cannot import site module的解决方法
exception in thread "main" importerror: cannot import site module and its dependencies: no module named site
public class jpythonscript { public static void main(string args[]) { properties props = new properties(); props.put("python.import.site", "false"); properties preprops = system.getproperties(); pythoninterpreter.initialize(preprops, props, new string[0]); pythoninterpreter interpreter = new pythoninterpreter(); interpreter.exec("days=('mod','tue','wed','thu','fri','sat','sun'); "); interpreter.exec("print days[1];"); }
// 进行复杂类接受处理 map<string, object> res = new hashmap<string, object>(); res.put("1", "danny"); res.put("2", "fanny"); pythoninterpreter interpm = new pythoninterpreter(); interpm.execfile("./src/com/datadeal.py"); pyfunction pyfunctionm = (pyfunction) interpm.get("main", pyfunction.class); map<pyobject, pyobject> tablem = new hashmap<pyobject, pyobject>(); tablem.put(new pystring("conf"), pyjavatype.wrapjavaobject(res)); pydictionary pydm = new pydictionary(tablem);
更多java相关内容感兴趣的读者可查看本站专题:《java数据结构与算法教程》、《java操作dom节点技巧总结》、《java文件与目录操作技巧汇总》和《java缓存操作技巧汇总》
希望本文所述对大家java程序设计有所帮助。
推荐阅读