Java调用Python
来源:互联网 发布:美国特效软件 编辑:程序博客网 时间:2024/05/20 08:27
Python代码:D: \\ test.py
def test(a, b): return a + bprint "Java To python";
Java代码:
import java.util.Properties;import org.python.core.PyFunction;import org.python.core.PyInteger;import org.python.core.PyObject;import org.python.util.PythonInterpreter;/** * Java调用Python代码 */public class JavaConPython{ public static void main(String[] args) { //Java运行Python文件 Properties props = new Properties(); 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 interpreter = new PythonInterpreter(); interpreter.execfile("D:\\test.py"); //Java调用Python方法 PyFunction func = (PyFunction) interpreter.get("test", PyFunction.class); int a = 2016, b = 2; PyObject pyobj = func.__call__(new PyInteger(a), new PyInteger(b)); String ret = "a+b = " + pyobj.toString(); System.out.println(ret); interpreter.close(); }}
运行结果:
Java To pythona+b = 2018
0 0
- Java调用Python
- java调用Python文件
- java 调用python
- python调用java
- python 调用Java
- java中调用python
- Java调用Python
- Java调用Python脚本
- Java直接调用Python
- Java调用Python
- python调用java
- Java调用python
- java调用python
- java 调用python
- python调用java函数
- java调用Python
- java中调用python
- Java调用Python脚本
- Hbase中checkAndPut操作
- 提高自己网速的方法
- clone-graph
- 第一次总结
- 总结
- Java调用Python
- 迷宫问题
- javaweb开发中的那些坑
- 自定义View之onDraw()
- 关于集合我个人的总结和理解
- jQuery选择器
- 推荐系统_FP-Tree算法
- 有关Android国际化的一点积累
- 博为峰Java技术文章 ——JavaSE Swing 标签