jython安装和使用

原文链接: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 plaincopy
  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. }  


原文地址:https://www.cnblogs.com/java20130723/p/3212044.html