What's So Good About Jython? - Jython Reuse From Java (Page 3 of 4 )
The example above has shown how you can access Java classes from Jython. Now I should explain how you can use Jython code from Java.
The answer really is quite simple. Since Jython is an implementation of Python in Java, it needs to compile the Python functions that you write down to Java bytecode before it can run them. Normally it avoids a lengthy compilation step by doing the compilation ‘on the fly’, but you can also compile your Jython classes down to Java .class files or .jar archives that you can place on the classpath of your ‘master’ application.
You perform the compilation by using the jythonc compiler provided with Jython. This compiler first generates Java source files from the Jython code., then compiles them using a standard Java compiler. If you generate .class files, then you need to remember to put the jython.jar runtime on the Java classpath when you run the master application, as the Java source files that jythonc generates have dependencies on the Jython runtime. If you generate a jar file using jythonc, however, there are options to include files from the Jython runtime within the jar. For example, if the Jython source code for the example given above is contained in the file MyExample.py, then it can be jar’ed up with the following command:
jythonc -c -j myjar.jar MyExample.py
This creates a jar file containing the compiled Java version of the Jython code we wrote as well as files from the Jython runtime. The jar file also contains a manifest, which makes the jar executable. You can run the jar by typing:
java -jar myjar.jar
Why Isn't Everybody Using Jython?
This is the hard part! Jython is an incredibly powerful tool, and I think there are many developers out there who would love to use it, if only they could. It’s one of those few languages that delights the developer, because the language is so powerful and you get results quickly. However, it is still very much a ‘niche’ language, used only by forward-thinking organisations and entrepreneurs. Here are some of the possible reasons for the low adoption rate of Jython:
Two Technologies, One Developer It won’t have escaped your notice that to understand a Jython program (or to be more precise a Jython program that uses Java classes) you need a solid understanding of both Java and Python. This places demands on the skills of the developer at a time when most managers prefer to simplify code development by choosing the technology that is the common denominator across their applications.
Learning Curve and Project Pressures Most developers come to Jython from Java, so have to learn about Python. They probably have to learn Python before being able to convince colleagues and managers that Jython will save time in the long run. And the time to learn a new language in the midst of project pressures and approaching deadlines is simply not available. It’s a vicious circle that needs to be broken, and I’m confident that for many, an investment in Jython will pay off.
Old Habits Die Hard If you’re a Java developer, you are probably quite satisfied developing Java. It’s a good, well established, programming language and you’re familiar with the idioms, patterns and style of the language that make it elegant. Why should you change? Well, I think you should at least consider using Jython, because firstly, Python, too, is a good programming language with a strong following. Secondly, a good developer should always be on the look-out for ways of becoming more productive, and I believe Jython is a good candidate. Lastly, technologies in the IT sector are always changing, so you should constantly re-evaluate your technology choices to be sure that you use your chosen technologies for the right reason, instead of ‘choosing’ them by default because “that’s what you do”.
Perception of an “Experimental Technology” Jython is not an “Experimental Technology” — It is industrial-strength and stable. If you’re not confident enough to use it for your main application straight away, then use it for writing test scripts and developing prototypes. Often, developers will begin with the intention of writing a prototype in Jython and later migrating to Java, only to find later that the migration is not necessary.
Jython is Slow To say that Jython is unsuitable for your application because it is an interpreted language and therefore too slow is almost certainly false. There are very few applications these days for which execution speed is such a major concern. And if you do find a bottleneck in your code you can always migrate that section of code to Java as necessary. As a developer, you should be more concerned about the speed of development than the speed of execution, and in this respect, Jython is fast!