Chapter 26. Extending and Embedding Jython

Jython implements the Python language on a Java Virtual Machine (JVM). Jython’s built-in objects, such as numbers, sequences, dictionaries, and files, are coded in Java. To extend Classic Python with C, you code C modules using the Python C API (as covered in Extending Python with Python’s C API). To extend Jython with Java, you do not have to code Java modules in special ways: every Java package on the Java CLASSPATH (or on Jython’s sys.path) is automatically available to your Jython scripts and Jython interactive sessions for use with the import statement covered in The import Statement. This automatic availability applies to Java’s standard libraries, third-party Java libraries you have installed, and Java classes you have coded yourself. You can extend Java with C using the Java Native Interface (JNI), and such extensions will be available to Jython code, just as if they were coded in pure Java rather than in JNI-compliant C.

For details on interoperation between Java and Jython, I recommend Jython Essentials, by Samuele Pedroni and Noel Rappin (O’Reilly). In this chapter, I offer a brief overview of the simplest interoperation scenarios, just enough for a large number of practical needs. In most cases, importing, using, extending, and implementing Java classes and interfaces in Jython just works. In some cases, however, you need to be aware of issues related to accessibility, type conversions, and overloading, as covered in this chapter. ...

Get Python in a Nutshell, 2nd Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.