java执行python脚本并传递参数_Java 调用Python脚本,实现带参数

public class JavaRunPython {

public static String ByJython(Map map,String filePath,String methodName) {

Properties props = new Properties();

props.put("python.console.encoding", "UTF-8");

// Used to prevent: console: Failed to install '': java.nio.charset.UnsupportedCharsetException: cp0.

props.put("python.security.respectJavaAccessibility", "false");

//don't respect java accessibility, so that we can access protected members on subclasses

props.put("python.import.site","false");

Properties preprops = System.getProperties();

PythonInterpreter.initialize(preprops, props, new String[0]);

PythonInterpreter interp= new PythonInterpreter();

// 1. Python面向函数式编程: 在Java中调用Python函数

//String pythonFunc = "D:\\javaPythonFile.py";

// 加载python程序

interp.execfile(filePath);

for (Map.Entry mp : map.entrySet()) {

interp.set(mp.getKey(), mp.getValue());

}

// System.setProperty("python.home", "");

// 调用Python程序中的函数

PyFunction pyf = interp.get(methodName, PyFunction.class);

//PyObject dddRes = pyf.__call__(Py.newString("zhangsan"), Py.newInteger(3));

PyObject pyObj = pyf.__call__();

String str = (String) pyObj.__tojava__(String.class);

pi1.cleanup();

pi1.close();

return str;

}

}

以上就是关于 Java 调用Python脚本,实现带参数 的全部内容了,喜欢的小伙伴别忘了点赞分享一下哦,关注优讯网,优讯有你更精彩!

转载自: https://my.oschina.net/lisc2016/blog/3115424

版权归原作者所有,如有侵权请联系我们删除。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值