jar 包下载
jython
实例
package com.curiousby.python.demo;
import org.python.core.PyFunction;
import org.python.core.PyInteger;
import org.python.core.PyObject;
import org.python.util.PythonInterpreter;
/**
* @author baoyou E-mail:curiousby@163.com
* @version 2016年8月1日 下午1:05:36
*
jython jython
2.1
*
* desc: ...
*/
public class PythonByJava {
public static void main(String[] args) {
PythonInterpreter interpreter = new PythonInterpreter();
interpreter.execfile("I:\\cache\\ea-ws\\DemoJava\\conf\\Test1.py");
PyFunction func = (PyFunction)interpreter.get("sum",PyFunction.class);
int a = 1, b = 2 ;
PyObject pyobj = func.__call__(new PyInteger(a), new PyInteger(b));
System.out.println(pyobj.toString());
}
}
结果:
3
其他两种 调用方法:
a) 直接执行Python脚本代码
PythonInterpreter interpreter = new PythonInterpreter();
interpreter.exec("days=('mod','Tue','Wed','Thu','Fri','Sat','Sun'); "); ///执行python脚本
b)使用Runtime.getRuntime()执行脚本文件
Process proc = Runtime.getRuntime().exec("python D:\\demo.py");
proc.waitFor();
更多技巧请《转发 + 关注》哦!
本文来自投稿,不代表本人立场,如若转载,请注明出处:http://www.sosokankan.com/article/1775133.html