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捐助),沒錢捧個人場,謝謝各位。

謝謝您的贊助,我會做的更好!