Python之玩转Jython系列(一)

来源:互联网 发布:域名备案注销 编辑:程序博客网 时间:2024/05/21 16:11

,java 调用jython

简单调用:

                PythonInterpreter interp1 = new PythonInterpreter();interp1.exec("import re");interp1.execfile("./src/com/configValue.py");PyFunction pyFunction1 = (PyFunction) interp1.get("jython里的方法名",PyFunction.class);System.out.println("config value: "+ pyFunction1.__call__(new PyString("MaxValue")));

但在项目中,这样会带来很多麻烦,特此修改了下,如下:

                PythonInterpreter interp = new PythonInterpreter();Map<PyObject, PyObject> bean = new HashMap<PyObject, PyObject>(); //取得根目录路径          String rootPath=getClass().getResource("../").getFile().toString();  String path =  rootPath + "xxx/xxx.py"; interp.execfile(path);PyFunction func = interp.get("jython里的方法名", PyFunction.class);bean.put(new PyString("key"),PyJavaType.wrapJavaObject(jythonMethod));PyDictionary pyDictionary = new PyDictionary(bean);obj = func.__call__(pyDictionary);return obj;
这样的话,就可以调用到对应的jython了

0 0