栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

Java与Python之间使用jython工具类实现数据交互

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Java与Python之间使用jython工具类实现数据交互

最近有个功能需要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;

public class JythonUtils {
 
 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;
 }
 
 public static PythonInterpreter loadPythonFile(PythonInterpreter interp, String filePath){
 interp.execfile(filePath);
 return interp;
 }
 
 public static PyFunction loadPythonFunc(PythonInterpreter interp, String functionName){
 //加载方法
   PyFunction func = (PyFunction) interp.get(functionName,PyFunction.class);
 return func;
 }
 
 public static PyObject execFunc(PyFunction func){
 PyObject pyobj = func.__call__();
 return pyobj;
 }
 
 public static String execFuncToString(PyFunction func){
 PyObject pyobj = execFunc(func);
 return (String) pyobj.__tojava__(String.class);
 }
 
 public static String execFuncToString2(PyFunction func, String paramName){ 
 PyObject pyobj = func.__call__(new PyString(paramName));
 return (String) pyobj.__tojava__(String.class);
 }
 
 public Integer execFuncToInteger(PyFunction func){
 PyObject pyobj = execFunc(func);
 return (Integer) pyobj.__tojava__(Integer.class);
 }
 
 public List execFuncToList(PyFunction func){
 PyObject pyobj = execFunc(func);
 return (List) pyobj.__tojava__(List.class);
 }
 
 public Map execFuncToMap(PyFunction func){
 PyObject pyobj = execFunc(func);
 return (Map) pyobj.__tojava__(Map.class);
 }
 public void execFuncToByParamsList(PyFunction func, List 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);
 }
}

总结

以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作具有一定的参考学习价值,谢谢大家对考高分网的支持。如果你想了解更多相关内容请查看下面相关链接

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/138837.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号