当前位置:主页 > 软件编程 > JAVA代码 >

Java调用python的方法(jython)

时间:2021-03-16 10:23:19 | 栏目:JAVA代码 | 点击:

1 什么是jython?

他其实是一门语言,并非是Java 或者Python的解释器.用它可以实现,java和python代码的互相访问。

2 简单的例子

java中执行python 语句

PythonInterpreter interpreter = new PythonInterpreter(); 
interpreter.exec("days=('mod','Tue','Wed','Thu','Fri','Sat','Sun'); "); 
interpreter.exec("print days;"); 

java调用python的脚本:

PythonInterpreter interpreter = new PythonInterpreter();  
 interpreter.execfile("script.py");  

java调用python类当中的函数

先在python文件中定一个python函数

def pluser(a,b): 
  # print "the result of pluser is %d" % (a+b) 
  return a+b 

在java当中去调用:

PythonInterpreter interpreter = new PythonInterpreter(); 
    interpreter.execfile("F:\\machine learning\\machinelearninginaction\\Ch02\\test.py"); 
    PyFunction function = (PyFunction)interpreter.get("pluser",PyFunction.class); 
    PyObject o = function.__call__(new PyInteger(8),new PyInteger(23)); 

您可能感兴趣的文章:

相关文章