jar 包下载
<dependency>
<groupId>jython</groupId>
<artifactId>jython</artifactId>
<version>2.1</version>
</dependency>
实例
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:[email protected]
* @version 2016年8月1日 下午1:05:36
*<pre>
<dependency>
<groupId>jython</groupId>
<artifactId>jython</artifactId>
<version>2.1</version>
</dependency>
*</pre>
* 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();
捐助开发者
在兴趣的驱动下,写一个
免费
的东西,有欣喜,也还有汗水,希望你喜欢我的作品,同时也能支持一下。 当然,有钱捧个钱场(右上角的爱心标志,支持支付宝和PayPal捐助),没钱捧个人场,谢谢各位。

谢谢您的赞助,我会做的更好!