---
title: "java Jython / JPython / "
author: "siemens800"
date: "2016-12-28T15:19:16Z"
tags: []
categories: ["编程语言"]
slug: "java-jython-jpython"
draft: false
lastmod: "2017-01-09T10:45:30Z"
id: 2347850
url: "https://lindows.iteye.com/blog/2347850"
---

s

http://www.jython.org

 

**Jython 简单入门**

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

**1. 用Jython调用Java类库** 

第一步、创建Java类 

写一个简单的Java类，用Point来示例： 

Java代码  [![收藏代码](http://willzh.iteye.com/images/icon_star.png)]()

01. import org.python.core.\*;
02. public class Point extends PyObject
03. {
04. private int x;
05. private int y;
06. public Point()
07. {
08. x = 0;
09. 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代码  [![收藏代码](http://willzh.iteye.com/images/icon_star.png)]()

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

第二步、简单调用 

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

Python代码  [![收藏代码](http://willzh.iteye.com/images/icon_star.png)]()

1. #!/usr/bin/env jython
2. import Point
3. if \_\_name\__ == "\_\_main\_\_":
4. p = Point(2,3)
5. p.dump()

第三步、扩展 

Python代码  [![收藏代码](http://willzh.iteye.com/images/icon_star.png)]()

01. import Point
02. class Circle(Point):
03. def \_\_init\_\_(self, x, y, r):
04. super(Circle, self).\_\_init\_\_(x, y)
05. self.r = r;
06. def dump(self):
07. super(Circle, self).dump()
08. print "This radius of Circle is %s" % self.r
09. 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代码  [![收藏代码](http://willzh.iteye.com/images/icon_star.png)]()

01. import org.python.core.PyException;
02. import org.python.core.PyInteger;
03. import org.python.core.PyObject;
04. import org.python.util.PythonInterpreter;
05. public class SimpleEmbedded {
06. public static void main(String\[] args) throws PyException {
07. PythonInterpreter interp = new PythonInterpreter();
08. interp.exec("import sys");
09. 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代码  [![收藏代码](http://willzh.iteye.com/images/icon_star.png)]()

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

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

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

Python代码  [![收藏代码](http://willzh.iteye.com/images/icon_star.png)]()

01. $ jython
02. Jython 2.2.1 on java1.6.0\_0
03. Type "copyright", "credits" or "license" for more information.
04. &gt;&gt;&gt; import sys
05. &gt;&gt;&gt; from java.net import URL
06. &gt;&gt;&gt; url = URL("http://willzh.iteye.com")
07. &gt;&gt;&gt; urlopen = url.openConnection()
08. &gt;&gt;&gt; input = urlopen.getInputStream()
09. &gt;&gt;&gt; c = input.read()
10. &gt;&gt;&gt; while c!=-1:
11. sys.stdout.write(chr(c))
12. c = input.read()

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

Python代码  [![收藏代码](http://willzh.iteye.com/images/icon_star.png)]()

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代码  [![收藏代码](http://willzh.iteye.com/images/icon_star.png)]()

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

**4. 将Python代码编译为Java类** 

第一步、创建Python类 Goo.py 

Python代码  [![收藏代码](http://willzh.iteye.com/images/icon_star.png)]()

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代码  [![收藏代码](http://willzh.iteye.com/images/icon_star.png)]()

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

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

Python代码  [![收藏代码](http://willzh.iteye.com/images/icon_star.png)]()

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