jython安装和使用

来源URL:http://blog.csdn.net/zhongweijian/article/details/4742549


1.从http://www.jython.org/Project/installation.html下载jython安装文件,运行命令“Java -jar jython_installer-2.5.0.jar,jython即安装成功.

   2.把jython安装目录加入到系统环境变量,在java工程中加入jython安装目录下的jython.jar即可在java中使用jython了。

   下面是我的实例,一个是在java中运行test.py脚本,一个是直接在java中直接运行Python命令。

 

[java]  view plain  copy
  1. import org.python.util.PythonInterpreter;  
  2. import org.python.core.*;  
  3. public class JythonTest {  
  4.     public static void main(String[] args) {  
  5.         //PythonInterpreter interp = new PythonInterpreter();  
  6.         //interp.execfile("test.py"); //运行test.py脚本  
  7.                //运行python命令  
  8.         PythonInterpreter interp =  
  9.             new PythonInterpreter();  
  10.             System.out.println("Hello, brave new world");  
  11.             interp.exec("import sys");  
  12.             interp.exec("print sys");  
  13.             interp.set("a"new PyInteger(42));  
  14.             interp.exec("print a");  
  15.             interp.exec("x = 2+2");  
  16.             PyObject x = interp.get("x");  
  17.             System.out.println("x: "+x);  
  18.             System.out.println("Goodbye, cruel world");  
  19.     }  
  20. }  


  • 5
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值