Java与Python之间使用jython工具类实现数据交互
程序员文章站
2024-03-31 14:43:10
最近有个功能需要java与python之间的数据交互,java需要把参数传给python,然后python计算的结果返回给java.于是就写了一个工具类.
首先,mave...
最近有个功能需要java与python之间的数据交互,java需要把参数传给python,然后python计算的结果返回给java.于是就写了一个工具类.
首先,maven 需要加载jython的依赖.工具类代码如下:
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); } }
总结
以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作具有一定的参考学习价值,谢谢大家对的支持。如果你想了解更多相关内容请查看下面相关链接