A bridge interface between Python and Java.
Project description
Rubicon-Java is a bridge between the Java Runtime Environment and Python. It enables you to:
Instantiate objects defined in Java,
Invoke static and instance methods on objects defined in Java,
Access and modify static and instance fields on objects defined in Java, and
Write and use Python implementations of interfaces defined in Java.
Quickstart
Rubicon-Java consists of three components:
A Python library,
A JNI library, and
A Java JAR file.
A Makefile has been provided to compile the JNI and JAR components. Type:
$ make
to compile them. The compiled output will be placed in the dist directory.
To use Rubicon-Java, you’ll need to ensure:
rubicon.jar is in the classpath when you start your Java VM.
The Rubicon library file is somewhere that it will be found by dynamic library discovery. This means:
Under OS X, put the directory containing librubicon.dylib is in your DYLD_LIBRARY_PATH
Under Linux, put the directory containing librubicon.so is in your LD_LIBRARY_PATH
Under Windows…. something :-)
The rubicon Python module is somewhere that can be added to a PYTHONPATH. You can install rubicon using:
$ pip install rubicon-java
If you do this, you’ll need to reference your system Python install when setting your PYTHONPATH.
The Rubicon bridge starts on the Java side. Import the Python object:
import org.pybee.rubicon.Python;
Then start the Python interpreter, and run a Python file:
# 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();
The PYTHONPATH you specify must enable access to the rubicon Python module.
In your Python script, you can then reference Java objects:
>>> 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
It’s also possible to provide implementations of Java Interfaces in Python. For example, lets say you want to create a Swing Button, and you want to respond to button clicks:
>>> 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)
Of course, this sample code won’t work unless it’s in the context of a larger application starting a Swing GUI and so on.
Testing
To run the Rubicon test suite:
Configure your shell environment so that the Python, Java, and Rubicon dynamic libraries can be discovered by the dynamic linker.
On OSX, using Python 2.7.7 built under Homebrew:
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
Build the libraries:
$ make clean $ make all
Run the test suite:
$ java org.pybee.rubicon.test.Test
This is a Python test suite, invoked via Java.
Community
Rubicon is part of the BeeWare suite. You can talk to the community through:
The BeeWare Users Mailing list, for questions about how to use the BeeWare suite.
The BeeWare Developers Mailing list, for discussing the development of new features in the BeeWare suite, and ideas for new tools for the suite.
Contributing
If you experience problems with this backend, log them on GitHub. If you want to contribute code, please fork the code and submit a pull request.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distributions
Hashes for rubicon_java-0.1.0-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 1dd54c0ee39dfd728f6582abee531621c4357880f785561c6b4fd07c612b6e54 |
|
MD5 | f17d02e037612099fc9c204f97e8e8fa |
|
BLAKE2b-256 | a956ff97b9ce1fff66e68f2c3f8f48f7e347e0129654af204c60c7820ee40c7b |
Hashes for rubicon_java-0.1.0-py2.py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 5a3e96b0ea08d1a728dc61d3b8b8a90458f9474bda62fb9018d3f2d1a9dfb72d |
|
MD5 | 9db1141164f1addfacf32beeebcf022d |
|
BLAKE2b-256 | 9c5bb6c01c9cfc7be830d7bb2525f58fa34b93d10067f07c3cfbd28246551d53 |