Java调用jython

15 篇文章 0 订阅

Java调用jython

因为工作需要,需要在Java Jvm 进程内调用Python脚本。下了Jython练练手,脚本语言看着真别扭啊。若干年前写自动化测试工具时也用过python一小阵子,但基本忘光光了。好了,直奔主题。

前提:

1. sun-jre1.6, jython 2.5

2. 在官网下下个jython_installer-2.5.0.jar,一路next, 在 /jython-install-path/里有个jython.jar, 把这个jython.jar import 进Java Project 里边。

python代码: fibo.py

[python]  view plain copy
  1.  # Fibonacci numbers module  
  2.   
  3. def fib(n):    # write Fibonacci series up to n  
  4.     a, b = 01  
  5.     while b < n:  
  6.         print b,  
  7.         a, b = b, a+b  
  8.   
  9. def fib2(n): # return Fibonacci series up to n  
  10.     result = []  
  11.     a, b = 01  
  12.     while b < n:  
  13.         result.append(b)  
  14.         a, b = b, a+b  
  15.     return result  


Java 代码:TestJython.java

[java]  view plain copy
  1. import org.python.core.PyException;  
  2. import org.python.core.PyFile;  
  3. import org.python.core.PyObject;  
  4. import org.python.core.PyString;  
  5. import org.python.util.PythonInterpreter;  
  6.   
  7. public class TestJython {  
  8.       
  9.     /** 
  10.      * 关注Jython这几个方面的内容: 
  11.      * 1. 怎样从某个指定的路径import一个Jython模块? 
  12.      * 2. 怎样调用模块里的方法? 
  13.      * 3. java 与 jython 间的参数应该怎样进行传递? 
  14.      *  
  15.      * @param args 
  16.      * @throws PyException 
  17.      */  
  18.     public static void main(String []args)throws PyException  
  19.     {  
  20.         PythonInterpreter interp = new PythonInterpreter();  
  21.           
  22.         // 1.   
  23.         // 引入系统模块,并将[./pythonsrc]加入模块搜索路径中去。  
  24.         interp.exec("import sys");  
  25.         interp.exec("sys.path.append('./pythonsrc')");  
  26.           
  27.         // 2. 引入 fibo, 执行fibo.fib(100), ok  
  28.         interp.exec("import fibo");  
  29.         interp.exec("fibo.fib(100)");  
  30.       
  31.         // 3. 在Java 程序中获得 python内置类型的变量值。  
  32.         String text = "'this is a bad day'";  
  33.         interp.exec("x = " + text + " + '!!!'");  
  34.         interp.exec("print x");  
  35.         PyObject object = interp.get("x");  
  36.         PyString xString = object.__str__();  
  37.         String outX = xString.asString();  
  38.           
  39.         interp.exec("_file = open('/home/fore/work/OaasSearch/search/oaas_search/workspace/oaas-search0.1/pythonsrc/fibo.py')");  
  40.         object = interp.get("_file");  
  41.         PyFile file = (PyFile)object;  
  42.         PyString content = file.read(100);  
  43.         System.out.println(content);  
  44.           
  45.         interp.exec("array = _file.read(100)");  
  46.         String array = interp.get("array").asString();  
  47.         System.out.println(array);  
  48.           
  49.         interp.exec("_file.close()");  
  50.         file.close();  
  51.     }  
  52. }  

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值