Java之——调用python方法

转载请注明出处:http://blog.csdn.net/l1028386804/article/details/50915556

今天,给大家介绍下如何在Java中调用Python方法。下面我们进入正题

1.在java类中直接执行python语句

[java]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. package com.lyz.test.jython;  
  2.   
  3. import org.python.util.PythonInterpreter;  
  4.   
  5. /** 
  6.  * 第一个Jython程序 
  7.  * @author liuyazhuang 
  8.  * 
  9.  */  
  10. public class FirstJythonScript {  
  11.       
  12.     public static void main(String args[]) {  
  13.         PythonInterpreter interpreter = new PythonInterpreter();  
  14.         interpreter.exec("days=('mod','Tue','Wed','Thu','Fri','Sat','Sun'); ");  
  15.         interpreter.exec("print days[1];");  
  16.     }  
  17. }  
这样得到的结果是Tue,在控制台显示出来,这是直接进行调用的。

2.在java中调用本机python脚本中的函数:

   首先建立一个python脚本,名字为:my_utils.py

[python]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. def adder(a, b):    
  2.    return a + b   
然后建立一个java类,用来测试,

[java]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. package com.lyz.test.jython.function;  
  2.   
  3. import org.python.core.PyFunction;  
  4. import org.python.core.PyInteger;  
  5. import org.python.core.PyObject;  
  6. import org.python.util.PythonInterpreter;  
  7.   
  8. /** 
  9.  * 在java中调用本机python脚本中的函数 
  10.  *  
  11.  * @author liuyazhuang 
  12.  * 
  13.  */  
  14. public class FirstJythonScript {  
  15.       
  16.     public static void main(String args[]) {  
  17.         PythonInterpreter interpreter = new PythonInterpreter();  
  18.         interpreter.execfile("D:/Workspaces/J2EE/Test/config/my_utils.py");  
  19.         PyFunction func = (PyFunction) interpreter.get("adder", PyFunction.class);  
  20.   
  21.         int a = 2010, b = 6;  
  22.         PyObject pyobj = func.__call__(new PyInteger(a), new PyInteger(b));  
  23.         System.out.println("anwser = " + pyobj.toString());  
  24.   
  25.     }  
  26. }  
得到的结果是:anwser = 2016

3.使用java直接执行python脚本

建立脚本input.py

[python]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1.  #open files    
  2.     
  3. print 'hello'    
  4. number=[3,5,2,0,6]    
  5. print number    
  6. number.sort()    
  7. print number    
  8. number.append(0)    
  9. print number    
  10. print number.count(0)    
  11. print number.index(5)   
建立java类,调用这个脚本
[java]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. package com.lyz.test.jython.script;  
  2.   
  3. import org.python.util.PythonInterpreter;  
  4.   
  5. /** 
  6.  * 使用java直接执行python脚本 
  7.  * @author liuyazhuang 
  8.  * 
  9.  */  
  10. public class FirstJythonScript {  
  11.     public static void main(String args[]) {  
  12.         PythonInterpreter interpreter = new PythonInterpreter();  
  13.         interpreter.execfile("D:/Workspaces/J2EE/Test/config/input.py");  
  14.     }  
  15. }  
结果:

[plain]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1.  hello    
  2. [3, 5, 2, 0, 6]    
  3. [0, 2, 3, 5, 6]    
  4. [0, 2, 3, 5, 6, 0]    
  5. 2    
  6. 3  
注:大家可以到链接http://download.csdn.net/detail/l1028386804/9464731下载所需要的jar包,到链接http://download.csdn.net/detail/l1028386804/9464750下载完整的示例程序
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值