Communication between Java and Python

18,426 views

Published on

TechTalk von Anastasia Eifer (DLR Köln-Porz, 03.06.2008) über das Zusammenspiel von Java und Python

Published in: Technology

Communication between Java and Python

  1. 1. TechTalk: Communication between Java and Python Anastasia Eifer, DLR Simulations- und Softwaretechnik 03.06.2008
  2. 2. Contents <ul><li>Introduction </li></ul><ul><li>Motivation </li></ul><ul><li>Communication through bindings (JPE, JPI) </li></ul><ul><li>Jython (JPython) </li></ul><ul><li>Jepp </li></ul><ul><li>JPype </li></ul><ul><li>Client-server communication (SPYRO, Pyro) </li></ul><ul><li>Summary </li></ul>
  3. 3. Introduction (1/4) <ul><li>Java </li></ul><ul><ul><li>Object-oriented </li></ul></ul><ul><ul><li>Platform-independent </li></ul></ul><ul><ul><li>Robust </li></ul></ul><ul><ul><li>Distributed </li></ul></ul><ul><ul><li>Secure </li></ul></ul><ul><ul><li>One of the most popular programming languages today </li></ul></ul>
  4. 4. Introduction (2/4) <ul><li>Python </li></ul><ul><ul><li>Dynamic multi-paradigm programming language </li></ul></ul><ul><ul><ul><li>Object-oriented </li></ul></ul></ul><ul><ul><ul><li>Functional </li></ul></ul></ul><ul><ul><ul><li>Aspect-oriented </li></ul></ul></ul><ul><ul><ul><li>Scripting language </li></ul></ul></ul><ul><ul><ul><li>… </li></ul></ul></ul><ul><ul><li>Remarkably clean syntax </li></ul></ul><ul><ul><li>Short development time </li></ul></ul><ul><ul><li>Extensive standard libraries </li></ul></ul><ul><ul><li>Highly readable language </li></ul></ul><ul><ul><li>Can be learned in a few days </li></ul></ul>
  5. 5. Introduction (3/4) <ul><li>&quot;Hello World!&quot;-Code </li></ul><ul><ul><li>Java </li></ul></ul><ul><ul><li>Python </li></ul></ul>print(&quot;Hello World!&quot;) public class HelloWorld {        public static void main(String[] args) {            System.out.println(&quot;Hello World!&quot;);        }   }  
  6. 6. Introduction (4/4) <ul><li>Print the integers from 1 to 9 </li></ul><ul><ul><li>Java </li></ul></ul><ul><ul><li>Python </li></ul></ul>for (int i = 1; i < 10; i++)  {   System.out.println(i);   }  for i in range (1,10):        print i 
  7. 7. Motivation <ul><li>Java and Python: a perfect couple </li></ul><ul><ul><li>Building software from existing components </li></ul></ul><ul><ul><li>Combination of a system programming language (Java) and a scripting language (Python) </li></ul></ul><ul><ul><ul><li>Rapid application development </li></ul></ul></ul><ul><ul><ul><li>Creating automated tests </li></ul></ul></ul><ul><ul><ul><li>Embedded scripting </li></ul></ul></ul>
  8. 8. Communication through bindings <ul><li>JPE </li></ul><ul><ul><li>Java-Python Extension </li></ul></ul><ul><ul><li>JNI-based wrapper </li></ul></ul><ul><ul><li>Integration of Java and standard Python (CPython) </li></ul></ul><ul><ul><li>http://jpe.sourceforge.net/ </li></ul></ul><ul><ul><li>JPE project does not appear to be active </li></ul></ul><ul><li>JPI </li></ul><ul><ul><li>Java-Python Interface </li></ul></ul><ul><ul><li>JNI-based wrapper </li></ul></ul><ul><ul><li>JPI project is no longer developed </li></ul></ul>
  9. 9. Jython (1/4) <ul><li>What is Jython? </li></ul><ul><ul><li>Complete re-implementation of the Python programming language in Java </li></ul></ul><ul><ul><li>Embedded scripting </li></ul></ul><ul><ul><li>Interactive experimentation </li></ul></ul><ul><ul><li>Rapid application development </li></ul></ul><ul><li>Home </li></ul><ul><ul><li>http://www.jython.org </li></ul></ul>
  10. 10. Jython (2/4) <ul><li>Interactive experimentation </li></ul><ul><ul><li>C:jython>jython </li></ul></ul><ul><ul><li>Jython 2.0 on java1.2.1 </li></ul></ul><ul><ul><li>Type &quot;copyright&quot;, &quot;credits&quot; or &quot;license&quot; for more information. </li></ul></ul><ul><ul><li>>>> from java.util import Random </li></ul></ul><ul><ul><li>>>> r = Random() </li></ul></ul><ul><ul><li>>>> r.nextInt()-790940041 </li></ul></ul><ul><ul><li>>>> for i in range(3): </li></ul></ul><ul><ul><li>...     print r.nextDouble() </li></ul></ul><ul><ul><li>... </li></ul></ul><ul><ul><li>0.23347681506123852 </li></ul></ul><ul><ul><li>0.8526595592189546 </li></ul></ul><ul><ul><li>0.3647833839988137 </li></ul></ul><ul><ul><li>>>> </li></ul></ul>
  11. 11. Jython (3/4) <ul><li>Running Python scripts </li></ul><ul><ul><li>import org.python.util.PythonInterpreter; </li></ul></ul><ul><ul><li>import org.python.core.*; </li></ul></ul><ul><ul><li>class TestPython { </li></ul></ul><ul><ul><li>public static void main ( String[] args ) { try { org.python.util.PythonInterpreter python = </li></ul></ul><ul><ul><li>new org.python.util.PythonInterpreter(); python.execfile ( &quot;testscript.py&quot; ); } catch ( Exception e ) { System.out.println ( &quot;An error was encountered.&quot; ); } </li></ul></ul><ul><ul><li>} </li></ul></ul><ul><ul><li>} </li></ul></ul>
  12. 12. Jython (4/4) <ul><li>Current state of development </li></ul><ul><ul><li>The current release is Jython-2.2.1 </li></ul></ul><ul><ul><li>It is equivalent to the 2.2 release of CPython </li></ul></ul><ul><ul><li>CPython 2.5 is targeted for the next release </li></ul></ul><ul><ul><li>Jython-2.2.1 supports a large majority of the standard Python library </li></ul></ul><ul><ul><li>Jython-2.2.1 doesn't support a number of Python's built-in modules ! </li></ul></ul><ul><ul><li>(e.g. those that are written in C for CPython) </li></ul></ul>
  13. 13. Jepp (1/4) <ul><li>What is Jepp? </li></ul><ul><ul><li>Java Embedded Python </li></ul></ul><ul><ul><li>Embeds CPython in Java </li></ul></ul><ul><ul><li>Communication through interfacing at the native level in both Virtual Machines </li></ul></ul><ul><li>Home </li></ul><ul><ul><li>http://jepp.sourceforge.net / </li></ul></ul>
  14. 14. Jepp (2/4) <ul><li>Using Jepp from within Java </li></ul><ul><ul><li>„ Hello World!“-Code </li></ul></ul><ul><ul><li>Running Python scripts </li></ul></ul><ul><ul><li>Jep jep = new Jep(); </li></ul></ul><ul><ul><li>jep.eval(&quot;print 'Hello World!'&quot;); </li></ul></ul><ul><ul><li>jep.close(); </li></ul></ul><ul><ul><li>Jep jep = new Jep(false); </li></ul></ul><ul><ul><li>jep.runScript(&quot;C:empestscript.py&quot;); </li></ul></ul><ul><ul><li>jep.close(); </li></ul></ul>
  15. 15. Jepp (3/4) <ul><li>Features </li></ul><ul><ul><li>Using the console ( console.py script) </li></ul></ul><ul><ul><li>Creating new exception types on the fly </li></ul></ul><ul><ul><li>$ java -jar jep.jar console.py </li></ul></ul><ul><ul><li>>>> from java.lang import * </li></ul></ul><ul><ul><li>>>> s = String('do crazy stuff') </li></ul></ul><ul><ul><li>>>> print String(s.toCharArray()[3:8]) </li></ul></ul><ul><ul><li>crazy </li></ul></ul><ul><ul><li>>>> from java.io import FileInputStream </li></ul></ul><ul><ul><li>>>> try: </li></ul></ul><ul><ul><li>... FileInputStream('no such file') </li></ul></ul><ul><ul><li>... except(jep.FileNotFoundException): </li></ul></ul><ul><ul><li>... print 'File not found.' </li></ul></ul><ul><ul><li>... </li></ul></ul><ul><ul><li>File not found. </li></ul></ul>
  16. 16. Jepp (4/4) <ul><li>Summary </li></ul><ul><ul><li>Very good scripting solution for Java </li></ul></ul><ul><ul><li>Run your existing Python scripts from Java </li></ul></ul><ul><ul><li>Access to high-quality Python extensions </li></ul></ul><ul><ul><li>Jepp's console provides an easy way to examine scripts and learn how to use new APIs </li></ul></ul>
  17. 17. JPype (1/3) <ul><li>What is JPype? </li></ul><ul><ul><li>JNI-based wrapper </li></ul></ul><ul><ul><li>Communication through interfacing at the native level in both Virtual Machines </li></ul></ul><ul><ul><li>Full access to java class libraries </li></ul></ul><ul><li>Home </li></ul><ul><ul><li>http://jpype.sourceforge.net / </li></ul></ul>
  18. 18. JPype (2/3) <ul><li>„ Hello World!“-Code </li></ul><ul><ul><li>from jpype import * </li></ul></ul><ul><ul><li>startJVM(path to jvm.dll, &quot;-ea&quot;) </li></ul></ul><ul><ul><li>java.lang.System.out.println(&quot;Hello World&quot;) </li></ul></ul><ul><ul><li>shutdownJVM() </li></ul></ul>
  19. 19. JPype (3/3) <ul><li>Running Python scripts </li></ul><ul><ul><li>import jpype </li></ul></ul><ul><ul><li>#start JVM with class path </li></ul></ul><ul><ul><li>startJVM(path to jvm.dll, &quot;-ea&quot;) </li></ul></ul><ul><ul><li>#create reference to java package </li></ul></ul><ul><ul><li>javaPackage = jpype.JPackage(&quot; JavaPackageName &quot; ) </li></ul></ul><ul><ul><li>#create reference to java class </li></ul></ul><ul><ul><li>javaClass = javaPackage. JavaClassName </li></ul></ul><ul><ul><li>#create instance of java class </li></ul></ul><ul><ul><li>javaObject = javaClass() </li></ul></ul><ul><ul><li>#call java methods </li></ul></ul><ul><ul><li>javaObject. JavaMethodName() </li></ul></ul><ul><ul><li>#shutdown JVM </li></ul></ul><ul><ul><li>jpype.shutdownJVM() </li></ul></ul>
  20. 20. Client-server communication <ul><li>SPIRO </li></ul><ul><ul><li>Simple Python Interface to Remote Objects </li></ul></ul><ul><ul><li>Handy bridge between CPython and Java (via Jython) </li></ul></ul><ul><ul><li>http://www.freenet.org.nz/python/spiro </li></ul></ul><ul><li>Pyro </li></ul><ul><ul><li>Python Remote Objects </li></ul></ul><ul><ul><li>http://pyro.sourceforge.net / </li></ul></ul>
  21. 21. Python Interpreter Python Interpreter Java Virtual Machine (JVM) Java Code (Anwendung) Jepp JNI Python Code JPype Jython Python Code Interface/Protokoll Python Code Python Code (Autor: Andreas Schreiber)
  22. 22. Summary <ul><li>Reimplementation of Python (Jython) </li></ul><ul><ul><li>(+) Very tight integration with Java </li></ul></ul><ul><ul><li>(+) One runtime environment </li></ul></ul><ul><ul><li>(-) Jython-2.2.1 doesn't support a number of Python's built-in modules, it is slower in comparison to CPython </li></ul></ul><ul><li>Communication through Virtual Machines (JPype, Jepp, JPE) </li></ul><ul><ul><li>(+) CPython‘s speed </li></ul></ul><ul><ul><li>(+) Access to high-quality Python extensions </li></ul></ul><ul><ul><li>(-) Two runtime environments </li></ul></ul><ul><li>Client-server communication </li></ul><ul><ul><li>(+) System- and language independence </li></ul></ul><ul><ul><li>(-) Significant overhead, development time-consuming </li></ul></ul>
  23. 23. Links <ul><li>Exploring Python </li></ul><ul><li>Markus Nix (Hrsg.), Markus Nix, Martin Grimme, Torsten Marek, Michael Weigend, Wolfgang Weitz ( Download ) </li></ul><ul><li>Communication between Java and Python </li></ul>

×