【转】Jython简单入门

时间:2023-03-10 05:06:39
【转】Jython简单入门

1. 用Jython调用Java类库

第一步、创建Java类

写一个简单的Java类,用Point来示例:

  1. import org.python.core.*;
  2. public class Point extends PyObject
  3. {
  4. private int x;
  5. private int y;
  6. public Point()
  7. {
  8. ;
  9. ;
  10. }
  11. public Point(int x, int y)
  12. {
  13. this.x = x;
  14. this.y = y;
  15. }
  16. public void dump()
  17. {
  18. System.out.printf("The position is (%s, %s)\n", x , y);
  19. }
  20. }

编译的时候,记得把jython.jar加入类环境中:

  1. export CLASSPATH=/usr/share/java/jython.jar
  2. javac Point.java

第二步、简单调用

现在可以编写Jython来调用上面的Java类库了

  1. #!/usr/bin/env jython
  2. import Point
  3. if __name__ == "__main__":
  4. )
  5. p.dump()

第三步、扩展

  1. import Point
  2. class Circle(Point):
  3. def __init__(self, x, y, r):
  4. super(Circle, self).__init__(x, y)
  5. self.r = r;
  6. def dump(self):
  7. super(Circle, self).dump()
  8. print "This radius of Circle is %s" % self.r
  9. def area(self):
  10. *3.1415
  11. if __name__ == "__main__":
  12. )
  13. p.dump()
  14. ) # 但是实际上这里有问题,我不知道怎么回事,还在研究中……哪位指点下
  15. c.dump()

虽然测试的时候有问题,但是隐约感觉到了Jython的强大,让我们欢呼一下吧

2. 用Java执行Python代码

在安装好的Demo里有个例子,可以拿出来炫炫

  1. import org.python.core.PyException;
  2. import org.python.core.PyInteger;
  3. import org.python.core.PyObject;
  4. import org.python.util.PythonInterpreter;
  5. public class SimpleEmbedded {
  6. public static void main(String[] args) throws PyException {
  7. PythonInterpreter interp = new PythonInterpreter();
  8. interp.exec("import sys");
  9. interp.exec("print sys");
  10. ));
  11. interp.exec("print a");
  12. interp.exec("x = 2 + 2");
  13. PyObject x = interp.get("x");
  14. System.out.println("x: " + x);
  15. }
  16. }

结果如下:

  1. $ javac SimpleEmbedded.java
  2. $ java SimpleEmbedded
  3. sys module
  4. $

3. 直接在Jython中使用Java内部的类库

如果你不介意,当然可以在Jython中交互执行Java的类库。下面是一个有点“实用”的例子:

  1. $ jython
  2. on java1.6.0_0
  3. Type "copyright", "credits" or "license" for more information.
  4. >>> import sys
  5. >>> from java.net import URL
  6. >>> url = URL("http://willzh.iteye.com")
  7. >>> urlopen = url.openConnection()
  8. >>> input = urlopen.getInputStream()
  9. >>> c = input.read()
  10. :
  11. sys.stdout.write(chr(c))
  12. c = input.read()

安装好的Demo里有些例子也可以参考,例如:

  1. from java.applet import Applet
  2. import sys
  3. class HelloWorld(Applet):
  4. def paint(self, g):
  5. )
  6. if __name__ == '__main__':
  7. import pawt
  8. pawt.test(HelloWorld())

直接调用就可以了:

  1. jython HelloJython.py # 假设上面代码的文件名是HelloJython.py
  2. java org.python.util.jython HelloJython.py # 也可以这样调用

4. 将Python代码编译为Java类

第一步、创建Python类 Goo.py

  1. import java.lang.Object
  2. class Goo(java.lang.Object):
  3. def __init__(self):
  4. '''''public Goo()'''
  5. def dump(self):
  6. '''''@sig public void dump()'''
  7. print 'goooooo'

注意函数文档中的字符串,public Goo()表示告诉Java这是构造函数。

第二步、创建Java测试类 GooTest.java

  1. public class GooTest {
  2. public static void main(String[] args)
  3. {
  4. Goo goo = new Goo();
  5. goo.dump();
  6. }
  7. }

第三步、编译 
终端下运行如下命令:

  1. # jythonc Goo.py
  2. # javac -cp jpywork:$CLASSPATH GooTest.java # 将jpywork路径加入搜索路径中
  3. # java -cp jpywork:$CLASSPATH GooTest
  4. goooooo

运行成功!

5. 后记

很多人说,Python比Java开发速度来的要快,但是Java也有Java不可动摇的强大之处,如此结合,想必Jython的好处和作用显而易见了吧。

本文转自:http://willzh.iteye.com/blog/307222