Every one of us who works with technology knows the feeling of finding a tool that perfectly fits the need for which it was designed. Jython is one of those perfect fits, combining the ease of Python scripting with the ubiquity and power of the Java libraries and Java Virtual Machine. Each of the book’s two authors had an “Aha!” experience discovering the power of Jython.

Samuele Pedroni’s moment came when a friend got him involved in a project about machine learning and artificial life. They originally planned to implement the project in Java. Java’s garbage collection and rich libraries meant that they could focus just on their ideas. The system needed to be portable, and had to allow for the distribution of the computational load over many machines, when necessary.

At the same time, Samuele was reading O’Reilly’s Learning Python and discovered what was then called JPython. The first thing he did with JPython was embed it in a CORBA server to manage startup configuration on his artificial life project. Samuele discovered that mixing Java and JPython gave him and his friend even more freedom to express their ideas.

Over time, they wrote a constellation of tools for their simulations in Jython. In the meantime, Samuele got involved in JPython development while it was transitioning from Jim Hugunin’s original JPython project to the “truly” open source Jython project.

Noel Rappin’s case is similar. He was working on a short program and wanted the flexibility and development speed of a scripting language like Python. However, the interface tools available didn’t provide exactly what the program needed (in particular, a nice, easy-to-use table display). The Java Swing toolkit had the needed functionality, but it came with the complexity overhead of Java and Swing as compared to, say, Python and wxWindows.

Well, you can probably see where this is going. He downloaded Jython and set about building a Swing interface to a Python program, and he was hooked almost immediately. The Swing interface was up and running in very little time, and the resulting code was amazingly clean compared to wxWindows, or Java and Swing.

For both authors, it quickly became clear that Jython was good for more than just small programs that need GUIs. The combination of Python as a development language and Java’s tool environment offers valuable leverage against a wide variety of development problems.

Jython Essentials is your guide to using Jython effectively for your development needs. The book provides an overview of the Python language and explains how Jython manages interaction between Python and Java code. Examples of using common Java libraries, such as the Swing API, Java servlets, and XML tools, are also provided.

Audience Expectations

Because Jython is a hybrid language, Jython users might come to it from a variety of different paths. Although we tried to make this book accessible to anyone who wants to learn Jython, some simplifying assumptions have been made.

We’ve assumed that you know enough Java that we do not need to explain basic Java or object-oriented programming concepts such as classes, types, constructors, loops, and so on. While you by no means need to be a Java guru to use this book, many of our definitions of Jython use Java functionality or syntax as a reference. We’ve also assumed that you are at least aware of the Java tools, libraries, and APIs discussed. Again, exhaustive knowledge is unnecessary.

Preexisting knowledge of Python is a benefit, but not a requirement. We’ve tried to explain enough of the Python language for you to use Python successfully. If you already know Python, you are ahead of the game. Appendix D, is a good starting point for exploring how Jython is different from standard Python. Those differences are elaborated in the initial chapters of this book (if you already know Python, most of the material in Chapter 2 through Chapter 5 will be familiar to you). To get the most out of Jython, you will probably want to learn more about the Java libraries using one of the references suggested in the Further Reading section of this Preface.


Jython Essentials has 13 chapters and 5 appendixes. Chapter 1 is an introduction to Jython, and contains an overview of the rest of the book. Chapter 2 through Chapter 5 comprise an overview of the Python language. Chapter 6 through Chapter 10 focus specifically on Jython concerns, such as interaction with existing Java code and creating Java executables. Chapter 11 through Chapter 13 offer a tour of Python and Java libraries and APIs of interest.

Chapter 1

Provides a quick overview of the goals and functionality of Jython and a road map to the rest of the book.

Chapter 2

Describes the Python language base types, such as integer, float, string, list, tuple, and dictionary, and of the built-in methods and operations of each type.

Chapter 3

Describes the statements used for flow control in Python, along with Python’s use of whitespace as a block delimiter.

Chapter 4

Describes the module as the basic unit of a Python program and explores the important Python concept of namespaces. Shows how to define, call, and use functions. Shows how to import existing Python and Java modules.

Chapter 5

Introduces Python’s object-oriented syntax and semantics. Explains how to create classes, construct instances, and make your new classes behave like built-in types.

Chapter 6

Explains how to use existing Java code from Jython. This includes Java object construction, Java method calling, automatic conversion between Java and Python types, and special handling of collection classes.

Chapter 7

Explores tighter Java/Jython integration by explaining how to subclass Java classes in your Jython code. Issues include overriding methods and access to the parent class.

Chapter 8

Discusses how Jython uses Java reflection to allow Java code to be accessed using common Python idioms. Jython’s uses of reflection are also discussed, including the list of Python’s reflection-enabling special variables and functions.

Chapter 9

Gives examples of how using Jython simplifies access to the Swing GUI toolkit.

Chapter 10

Presents examples of using specific Java tools from within Jython. The libraries used are the JDBC database library, the servlet API, and various XML parsing tools.

Chapter 11

Discusses several useful standard Jython modules, including modules for system access, regular expression matching, and unit testing.

Chapter 12

Discusses how to enhance a Java program by embedding Jython within it, allowing the Jython interpreter to be called programmatically.

Chapter 13

Covers the jythonc tool for creating Java .class and .jar files. The files created with jythonc can be used by any tool expecting to run a compiled Java file.

Appendix A

Explains how to acquire and install Jython.

Appendix B

Provides a complete list of command-line options for Jython and jythonc. Also includes information about the Jython registry.

Appendix C

Provides a list of Jython’s built-in exceptions.

Appendix D

Presents the complete list of differences between Jython and the original C implementation of Python.

Appendix E

Presents some of the most significant syntactic differences between Java and Python in a tabular format.

Further Reading

This book touches on Java and Python tools, which are covered in more detail in other O’Reilly books. Much of the content of these books is directly relevant to Jython. In particular, the following books might be particularly useful for a Jython programmer:

Learning Java, by Pat Niemeyer and Jonathan Knudsen or Learning Python, by Mark Lutz and David Ascher. Choose one based, of course, on which language you feel the need to learn more about.

Java & XML, by Brett McLaughlin, has a description of a number of more advanced XML tools we could not cover in this book. Python & XML, by Christopher A. Jones and Fred L. Drake, Jr., does the same from the Python side.

Java in a Nutshell, by David Flanagan, provides a good overview of Java concepts. Python in a Nutshell, by Alex Martelli, has not been published as this is written.

Java Servlet Programming, by Jason Hunter with William Crawford, talks about servlet programming in more detail.

Java Swing, by Robert Eckstein, Marc Loy, and Dave Wood, and Java Foundation Classes in a Nutshell, by David Flanagan, are both definitive references to Swing. The Eckstein book goes into much more detail on Swing concepts, while the Flanagan book is more of a reference, and also covers Java 2D.

Python Standard Library, by Fredrik Lundh, is a complete overview of what comes with a standard Python distribution. Much of this is also applicable to Jython.

Programming Python, by Mark Lutz, is a thorough rundown of Python programming techniques. Again, not all of this will apply to Jython, but it’s still a very useful book.

Conventions Used in This Book

Throughout this book, the use of the name “Python” refers to information common to both Jython and the original implementation of Python. Using the name “Jython” is a signal that the concept is applicable only to Jython. Descriptions of the original C-language implementation are noted as “CPython.” For example, “Python uses indentation to determine block boundaries, Jython lets you subclass Java classes, and CPython compiles to .pyc files.”

The following typographical conventions are used throughout this book:


Used to indicate commands, URLs, filenames, file extensions, directory or folder names, and UNC pathnames, as well as new terms where they are defined.

Constant width

Used for all code listings, and to designate anything that would appear literally in a Jython or Java program. This includes module names, method names, class names, function names, statements, and HTML tags.

Constant width bold

Used to indicate output from executed programs. Also used to mark lines in a code listing of special interest.

Constant width italic

Used for general placeholders that indicate that an item should be replaced by some actual value in your own program.


This icon indicates a tip, suggestion, or general note.


This icon indicates a warning or caution.

How to Contact Us

We have tested and verified all the information in this book to the best of our abilities, but you may find that features have changed or that we have let errors slip through the production of the book. Please let us know of any errors that you find, as well as suggestions for future editions, by writing to:

O’Reilly & Associates, Inc.
1005 Gravenstein Highway North
Sebastopol, CA 95472
(800) 998-9938 (in the United States or Canada)
(707) 829-0515 (international/local)
(707) 829-0104 (fax)

To ask technical questions or comment on the book, send email to:

We have a web site for the book, where we’ll list examples, errata, and any plans for future editions. You can access this page at:

For more information about books, conferences, Resource Centers, and the O’Reilly Network, see the O’Reilly web site at:

Samuele Pedroni’s Acknowledgments

For me, this book is a kind of portrait of Jython by a maintainer (me). I learned a lot digging a bit more into Jython for this book. I hope you will profit from my journey, too.

First, I would like to thank our editor, Laura Lewin, for asking me in the first place whether I would enjoy writing this book. Indeed I did.

To my coauthor, Noel Rappin, goes the credit for most of the concrete examples in this book. Thanks, Noel, for help with the writing, for all the contributions to the material, and for bearing my bit of pedantry and putting in the right dose of pragmatism.

My acknowledgments go to Guido van Rossum for inventing Python, to Jim Hugunin for the Java implementation of Python we still hack on, to Barry Warsaw for the work on JPython and for making a “truly” open source Jython possible, and to Finn Bock (the current primary maintainer) for his past work, for trusting me, and for keeping Jython real.

Thanks to everybody who has contributed to Jython and to everybody who actually uses it or will use it.

Thanks to the community supporting Python, to the python-dev people, and to Guido van Rossum again for supporting Jython, and for their more-witty-than-dry list.

Finally I would like to thank—in my peculiar abstract way—all the people who have been supportive and inspiring for me in the last years. With them, I share a part of my enthusiasm for this accomplished work.

This book is dedicated to its readers.

Noel Rappin’s Acknowledgments

I’ve had a lot of fun working on this book, and I’d like to thank a few people for making it possible.

First, thanks to Samuele Pedroni for being so open to my participation in this project, and for staying up late so that we could collaborate despite being six time zones apart. Laura Lewin, this book’s editor, has been great to work with, and she has displayed a wonderful sense of how the book should read, line by line—if you find the text clear and comprehensible, thank Laura. It has been great to work with both of you.

Without Mark Guzdial’s encouragement and advice, I would never have been in a position to work on this book in the first place. The technical reviewers, Finn Bock, Paula Ferguson, Clint Howarth, Jim Hugunin, Tom Kincaid, Alex Martelli, and Brian Zimmer, were extremely helpful in catching errors that got by us, and in keeping the book on track.

From a technical perspective, thanks to all the people who spent time developing the software critical to this book. Thanks also to Jim Hugunin, Barry Warsaw, Finn Bock, and Samuele, and the other contributors for their wonderful work on Jython, and to Guido van Rossum, Tim Peters, and the entire CPython team for everything they’ve done to build this exemplary tool and for their support of Jython. I’d also like to mention Slava Pestov, who designed the jEdit text editor on which much of the code for this book was written, for a great tool for both Java and Python editing.

Personally, I want to thank Amy McCulloch, who was my inspiration as a writer. She’d be thrilled to see me published and bored to tears by the subject matter. I also want to thank my parents, Donna and Donnie, for their unflagging support; my wife, Erin, who is the best part of my life, who is always willing to help me, and whose enthusiasm and encouragement are so important to me. And, last but not least, my daughter, Emma, whose main contribution was falling asleep early enough so that Daddy could have time to write.

Get Jython Essentials now with the O’Reilly learning platform.

O’Reilly members experience live online training, plus books, videos, and digital content from nearly 200 publishers.