最近有个功能需要java与python之间的数据交互,java需要把参数传给python,然后python计算的结果返回给java.于是就写了一个工具类.
首先,maven 需要加载jython的依赖.工具类代码如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 |
import java.util.list; import java.util.map; import java.util.properties; import org.apache.poi.ss.formula.functions.t; import org.python.core.pyfunction; import org.python.core.pyinteger; import org.python.core.pyobject; import org.python.core.pystring; import org.python.util.pythoninterpreter; /** * @classname: jythonutils * @description:todo(jython 工具类) * @author: zy * @date: * * @copyright: 2018 inc. all rights reserved. * 注意: */ public class jythonutils { /** * @title: jythoninit * @description: todo(初始化jython) * @param: @return * @return: pythoninterpreter * @throws */ public static pythoninterpreter jythoninit(){ //初始化site 配置 properties props = new properties(); props.put( "python.home" , "" ); //python lib 或 jython lib,根据系统中该文件目录路径 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 对象 pythoninterpreter interp = new pythoninterpreter(); return interp; } /** * @title: loadpythonfile * @description: todo(加载python 源码文件,) * @param: @param interp * @param: @param filepath ,比如:f:\\jpython_jar\\jpythontest\\pythontest.py 或/testpython/test.py * @param: @return * @return: pythoninterpreter * @throws */ public static pythoninterpreter loadpythonfile(pythoninterpreter interp, string filepath){ interp.execfile(filepath); return interp; } /** * @title: loadpythonfunc * @description: todo(加载python 源码文件中的某个方法) * @param: @param interp * @param: @param functionname * @param: @return * @return: pyfunction * @throws */ public static pyfunction loadpythonfunc(pythoninterpreter interp, string functionname){ //加载方法 pyfunction func = (pyfunction) interp.get(functionname,pyfunction. class ); return func; } /** * @title: execfunc * @description: todo(执行无参方法,返回pyobject) * @param: @param func * @return: pyobject * @throws */ public static pyobject execfunc(pyfunction func){ pyobject pyobj = func.__call__(); return pyobj; } /** * @title: execfunctostring * @description: todo(执行无参方法,返回一个字符串) * @param: @param func * @param: @return * @return: string * @throws */ public static string execfunctostring(pyfunction func){ pyobject pyobj = execfunc(func); return (string) pyobj.__tojava__(string. class ); } /** * @title: execfunctostring * @description: todo(执行有参方法,返回一个字符串) * @param: @param func * @param: @param paramname ,参数名 * @param: @return * @return: string * @throws */ public static string execfunctostring2(pyfunction func, string paramname){ pyobject pyobj = func.__call__( new pystring(paramname)); return (string) pyobj.__tojava__(string. class ); } /** * @title: execfunctointeger * @description: todo(执行无参方法,返回一个integer) * @param: @param func * @param: @return * @return: integer * @throws */ public integer execfunctointeger(pyfunction func){ pyobject pyobj = execfunc(func); return (integer) pyobj.__tojava__(integer. class ); } /** * @title: execfunctolist * @description: todo(执行无参方法,返回一个list) * @param: @param func * @param: @return * @return: list<t> * @throws */ public list<t> execfunctolist(pyfunction func){ pyobject pyobj = execfunc(func); return (list<t>) pyobj.__tojava__(list. class ); } /** * @title: execfunctomap * @description: todo(执行无参方法,返回一个map<string, object>) * @param: @param func * @param: @return * @return: map<string,object> * @throws */ public map<string, object> execfunctomap(pyfunction func){ pyobject pyobj = execfunc(func); return (map<string, object>) pyobj.__tojava__(map. class ); } public void execfunctobyparamslist(pyfunction func, list<t> paramslist){ } public static void main(string[] args){ pythoninterpreter interp = jythoninit(); //文件名 string filepath = "f:\\jpython_jar\\jpythontest\\pythontest.py" ; interp = loadpythonfile(interp, filepath); //函数名 string functionname = "count" ; pyfunction func = loadpythonfunc(interp, functionname); //执行无参方法,返回pyobject pyobject pyobj = execfunc(func); //执行无参方法,返回string string resultstr = execfunctostring(func); //执行有参方法,返回string string paramname = "name" ; string resultstr2 = execfunctostring2(func, paramname); } } |
总结
以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作具有一定的参考学习价值,谢谢大家对的支持。如果你想了解更多相关内容请查看下面相关链接
原文链接:https://blog.csdn.net/cafebar123/article/details/79394431
查看更多关于Java与Python之间使用jython工具类实现数据交互的详细内容...