ITeye博客

java Jython / JPython /

siemens800 · 2016-12-28 15:19 · Java · 编程语言 · Markdown

s

http://www.jython.org

 

Jython 简单入门

http://willzh.iteye.com/blog/307222

1. 用Jython调用Java类库 

第一步、创建Java类 

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

Java代码  收藏代码

  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. x = 0;
  9. y = 0;
  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加入类环境中: 

Shell代码  收藏代码

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

第二步、简单调用 

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

Python代码  收藏代码

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

第三步、扩展 

Python代码  收藏代码

  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. return self.r**2*3.1415
  11. if __name__ == "__main__":
  12. p = Point(2,3)
  13. p.dump()
  14. c = Circle(2,10,3) # 但是实际上这里有问题,我不知道怎么回事,还在研究中……哪位指点下
  15. c.dump()

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

2. 用Java执行Python代码 

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

Java代码  收藏代码

  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. interp.set("a", new PyInteger(42));
  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. }

结果如下: 

Shell代码  收藏代码

  1. $ javac SimpleEmbedded.java
  2. $ java SimpleEmbedded
  3. sys module
  4. 42
  5. x: 4
  6. $

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

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

Python代码  收藏代码

  1. $ jython
  2. Jython 2.2.1 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. >>> while c!=-1:
  11. sys.stdout.write(chr(c))
  12. c = input.read()

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

Python代码  收藏代码

  1. from java.applet import Applet
  2. import sys
  3. class HelloWorld(Applet):
  4. def paint(self, g):
  5. g.drawString("Hello from Jython %s!" % sys.version, 20, 30)
  6. if __name__ == '__main__':
  7. import pawt
  8. pawt.test(HelloWorld())

直接调用就可以了: 

Shell代码  收藏代码

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

4. 将Python代码编译为Java类 

第一步、创建Python类 Goo.py 

Python代码  收藏代码

  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 

Java代码  收藏代码

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

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

Python代码  收藏代码

  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的好处和作用显而易见了吧。 

 

 

end

评论(0)

暂无评论。

评论功能维护中,当前仅展示历史评论。