| 1 |
############ |
| 2 |
Introduction |
| 3 |
############ |
| 4 |
|
| 5 |
jpy is a **bi-directional** Python-Java bridge which you can use to embed Java code in Python programs or the other |
| 6 |
way round. It has been designed particularly with regard to maximum data transfer speed between the two languages. |
| 7 |
It comes with a number of outstanding features: |
| 8 |
|
| 9 |
* Fully translates Java class hierarchies to Python |
| 10 |
* Transparently handles Java method overloading |
| 11 |
* Support of Java multi-threading |
| 12 |
* Fast and memory-efficient support of primitive Java array parameters via `Python buffers <http://docs.python.org/3.3/c-api/buffer.html>`_ |
| 13 |
(e.g. `numpy arrays <http://docs.scipy.org/doc/numpy/reference/arrays.html>`_) |
| 14 |
* Support of Java methods that modify primitive Java array parameters (mutable parameters) |
| 15 |
* Java arrays translate into Python sequence objects |
| 16 |
* Java API for accessing Python objects (``jpy.jar``) |
| 17 |
|
| 18 |
jpy has been tested with Python 2.7, 3.3, 3.4 and Oracle Java 7 and 8 JDKs. It will presumably also work with Python 2.6 or |
| 19 |
3.2 and a Java 6 JDK. |
| 20 |
|
| 21 |
The initial development of jpy has been driven by the need to write Python extensions to an established scientific |
| 22 |
imaging application programmed in Java, namely the `BEAM <http://www.brockmann-consult.de/beam/>`_ toolbox |
| 23 |
funded by the European Space Agency (ESA). |
| 24 |
Writing such Python plug-ins for a Java application usually requires a bi-directional communication between Python and |
| 25 |
Java since the Python extension code must be able to call back into the Java APIs. |
| 26 |
|
| 27 |
|
| 28 |
************ |
| 29 |
How it works |
| 30 |
************ |
| 31 |
|
| 32 |
The jpy Python module is entirely written in the C programming language. The same resulting shared library is used |
| 33 |
as a Python jpy module and also as native library for the Java library (``jpy.jar``). |
| 34 |
|
| 35 |
Python programs that import the ``jpy`` module can load Java classes, access Java class fields, and call class |
| 36 |
constructors and methods. |
| 37 |
|
| 38 |
Java programs with ``jpy.jar`` on the classpath can import Python modules, access module attributes such as class |
| 39 |
types and variables, and call any callable objects such as module-level functions, class constructors, as well as |
| 40 |
static and instance class methods. |
| 41 |
|
| 42 |
|
| 43 |
Calling Java from Python |
| 44 |
======================== |
| 45 |
|
| 46 |
Instantiate Python objects from Java classes and call their public methods and fields:: |
| 47 |
|
| 48 |
import jpy |
| 49 |
|
| 50 |
File = jpy.get_type('java.io.File') |
| 51 |
|
| 52 |
file = File('test/it') |
| 53 |
name = file.getName() |
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
Calling Python from Java |
| 58 |
======================== |
| 59 |
|
| 60 |
Access Python attributes and call Python functions from Java:: |
| 61 |
|
| 62 |
PyModule sys = PyModule.importModule("sys"); |
| 63 |
PyObject path = sys.getAttribute("path"); |
| 64 |
path.call("append", "/usr/home/norman/"); |
| 65 |
String value = path.getStringValue(); |
| 66 |
|
| 67 |
|
| 68 |
Implementing Java interfaces using Python |
| 69 |
========================================= |
| 70 |
|
| 71 |
With jpy you can implement Java interfaces using Python. We instantiating Java (proxy) objects from Python modules or |
| 72 |
classes. If you call methods of the resulting Java object, jpy will delegate the calls to the matching Python |
| 73 |
module functions or class methods. Here is how this works. |
| 74 |
|
| 75 |
Assuming we have a Java interface ``PlugIn.java`` :: |
| 76 |
|
| 77 |
public interface PlugIn { |
| 78 |
String[] process(String arg); |
| 79 |
} |
| 80 |
|
| 81 |
and a Python implementation ``bibo_plugin.py`` :: |
| 82 |
|
| 83 |
class BiboPlugIn: |
| 84 |
def process(self, arg): |
| 85 |
return arg.split(); |
| 86 |
|
| 87 |
|
| 88 |
then we can call the Python code from Java as follows :: |
| 89 |
|
| 90 |
// Import the Python module |
| 91 |
PyModule plugInModule = PyLib.importModule("bibo_plugin"); |
| 92 |
|
| 93 |
// Call the Python class to instantiate an object |
| 94 |
PyObject plugInObj = plugInModule.call("BiboPlugin"); |
| 95 |
|
| 96 |
// Create a Java proxy object for the Python object |
| 97 |
PlugIn plugIn = plugInObj.createProxy(PlugIn.class); |
| 98 |
|
| 99 |
String[] result = plugIn.process('Abcdefghi jkl mnopqr stuv wxy z'); |
| 100 |
|
| 101 |
|
| 102 |
******************* |
| 103 |
Current limitations |
| 104 |
******************* |
| 105 |
|
| 106 |
* Java non-final, static class fields are currently not supported: |
| 107 |
The reason is that Java classes are represented in jpy's Python API as dynamically allocated, built-in |
| 108 |
extension types. Built-in extension types cannot have (as of Python 3.3) static, computed |
| 109 |
attributes which we would need for getting/setting Java static class fields. |
| 110 |
* Public final static fields are represented as normal (non-computed) type attributes: |
| 111 |
Their values are Python representations of the final Java values. The limitation here is, that they |
| 112 |
can be overwritten from Python, because Python does not know final/constant attributes. This could |
| 113 |
only be achieved with computed attributes, but as said before, they are not supported for |
| 114 |
built-in extension types. |
| 115 |
* It is currently not possible to shutdown the Java VM from Python and then restart it. |
| 116 |
|
| 117 |
|
| 118 |
******************************** |
| 119 |
Other projects with similar aims |
| 120 |
******************************** |
| 121 |
|
| 122 |
* `JPype <http://jpype.sourceforge.net/>`_ - allow python programs full access to java class libraries |
| 123 |
* `Jython <http://www.jython.org/>`_ - Python for the Java Platform |
| 124 |
* `JyNI <http://jyni.org/>`_ - Jython Native Interface |
| 125 |
* `Jynx <https://code.google.com/p/jynx/>`_ - improve integration of Java with Python |