Java中调用Python代码

import org.python.core.PyFunction;
import org.python.core.PyInteger;
import org.python.core.PyObject;
import org.python.util.PythonInterpreter;

public class FirstJavaScript
{
    public static void main(String args[])
    {
        PythonInterpreter interpreter = new PythonInterpreter();
        interpreter.exec("days=('mod','Tue','Wed','Thu','Fri','Sat','Sun'); ");
        interpreter.exec("print days[1];");
       
        System.out.println("----------------------------------");
       
        interpreter.execfile("tz.py"); 
        PyFunction func = (PyFunction)interpreter.get("adder",PyFunction.class); 
 
        int a = 2010, b = 2 ; 
        PyObject pyobj = func.__call__(new PyInteger(a), new PyInteger(b)); 
        System.out.println("anwser = " + pyobj.toString()); 
       
        System.out.println("----------------------------------");
       
        interpreter.execfile("test.py"); 
    }
}

附件已经加入 jython.jar

猜你喜欢

转载自zhangzhenting.iteye.com/blog/2298810