Chapter 13. Compiling Jython
All the uses of Jython that we have explored so far have required the use of the Jython interpreter, either directly through the interactive prompt, by launching the interpreter on a Jython script file, or by embedding the interpreter in a Java program. This approach makes perfect sense for standalone programs, but sometimes you need to run Jython code in a context where plain .class files are required and the standalone interpreter is not available. This requirement is true for many Java technologies, such as applets. To allow for this, Jython provides the jythonc static compilation tool, which compiles your Jython code to standalone Java-compiled .class files that can be distributed and used just as if they were originally written in Java.
Compiling Jython code allows you to use your Jython program as an applet class, bean class, and so on. You can also package your Jython program as a standalone .jar archive for distribution. Your compiled Jython classes can also be used and subclassed by a separate Java program. This chapter will explore the uses of jythonc to expand the reach of your Jython code.
One of the first questions to answer about using a static compilation tool for Jython is why it is necessary to do so at all. After all, the Jython interpreter clearly creates .class files (actually $py.class files) when a Jython module is loaded. Why can’t these .class files be used for distribution?
The answer has to do with how the interpreter ...