Python和Java之间的一个桥接口。
rubicon-java的Python项目详细描述
rubicon java是java运行时环境和python之间的桥梁。 它使您能够:
- 实例化Java中定义的对象,
- 对Java中定义的对象调用静态和实例方法
- 访问和修改Java中定义的对象上的静态字段和实例字段,以及
- 编写并使用用java定义的接口的python实现。
快速启动
rubicon java包含三个组件:
- 一个python库,
- 一个jni库,并且
- 一个java jar文件。
已经提供了Makefile来编译jni和jar组件。类型:
$ make
编译它们。编译后的输出将放在dist目录中。
跨平台支持
这个makefile目前只在os/x下工作;但是,build命令 并不复杂;应该很容易在另一个 平台。欢迎通过Pull请求创建Makefile跨平台
要使用Rubicon Java,您需要确保:
rubicon.jar在启动Java虚拟机时位于类路径中。
rubicon库文件位于动态 图书馆发现。这意味着:
- 在OSX下,将包含^{TT5}$的目录放入^{TT6}$
- 在linux下,将包含librubicon.so的目录放入LD_LIBRARY_PATH
- 在窗户下某物:—)
rubiconpython模块可以添加到 PYTHONPATH。您可以使用以下命令安装rubicon:
$ pip install rubicon-java
如果这样做,则需要在 设置您的PYTHONPATH。
rubicon桥从java端开始。导入python对象:
import org.pybee.rubicon.Python;
然后启动python解释器,运行python文件:
# Initialize the Python VM String pythonHome = "/path/to/python"; String pythonPath = "/path/to/dir1:/path/to/dir2"; if (Python.start(pythonHome, pythonPath, null) != 0) { System.out.println("Error initializing Python VM."); } # Start a Python script if (Python.run("/path/to/script.py") != 0) { System.out.println("Error running Python script."); } # Shut down the Python VM. Python.stop();
指定的PYTHONPATH必须启用对rubiconPython的访问 模块。
在python脚本中,您可以引用java对象:
>>> from rubicon.java import JavaClass # Wrap a Java class >>> URL = JavaClass("java/net/URL") # Then instantiate the Java class, using the API # that is exposed in Java. >>> url = URL("http://pybee.org") # You can then call methods on the Java object as if it # were a Python object. >>> print url.getHost() pybee.org
还可以在python中提供java接口的实现。 例如,假设您想要创建一个Swing按钮,并且您想要 响应按钮点击:
>>> from rubicon.java import JavaClass, JavaInterface # Wrap the Java interface >>> ActionListener = JavaInterface('java/awt/event/ActionListener') # Define your own implementation >>> class MyActionListener(ActionListener): ... def actionPerformed(self, event): ... print "Button Pressed" # Instantiate an instance of the listener >>> listener = MyActionListener() # Create a button, and set the listener >>> Button = JavaClass('javax/swing/JButton') >>> button = Button('Push it') >>> button.setActionListener(listener)
当然,除非在更大的 应用程序启动swing gui等等。
测试
运行rubicon测试套件:
配置shell环境,以便python、java和rubicon 动态链接器可以发现动态库。
在OSX上,使用自制的Python2.7.7:
export DYLD_LIBRARY_PATH=/usr/local/Cellar/python/2.7.7_2/Frameworks/Python.framework/Versions/2.7/lib/:`/usr/libexec/java_home`/jre/lib/server:./dist
构建库:
$ make clean $ make all
运行测试套件:
$ java org.pybee.rubicon.test.Test
这是一个python测试套件,通过java调用。
社区
rubicon是BeeWare suite的一部分。您可以通过以下方式与社区对话:
- @pybeeware on Twitter
- 关于如何使用beeware套件的问题。
- 用于讨论beeware套件中新特性的开发的BeeWare Developers Mailing list,以及该套件的新工具的想法。
贡献
如果您在这个后端遇到问题,log them on GitHub。如果你 想要贡献代码,请fork the code和submit a pull request。