Skip to main content

Jython : Scripting for the Java language

Posted by lucastorri on September 24, 2007 at 4:18 PM PDT

Scripts languages are famous for their simplicity and ease of development. For this, they can be powerful Allies to software development.

In this vast world of available scripting languages, one that really calls attention is Jython[http://www.jython.org]. Jython, is the Java implementation for the high-level Python language, allowing you to run Python on any Java platform.

Python is a multi paradigm language, can be learned in a few days and used to write any kind of application. Also, many programmers report substantial productivity gains using it.

The syntax is a little bit different from usual languages, cause uses indentation to separate blocks of code, which makes Python a highly readable language. An increase in indentation comes after certain statements and a decrease in indentation signifies the end of the current block.

For me, the biggest advantage of using Jython is that it provides interactivity with Java Classes and Objects. This property, makes possible, together with the interactive, interact with Java resources, allowing programmers to experiment and debug any Java system using Jython.

Installation is made through a GUI installer, making this process very simple. After that, you can run the Jython interpreter running the executable file in the installation folder. Once the intrepeter it's open, you can starting coding like in every other editor, but in each new line the code will be instantly interpreted. For example, look how easily I create a JFrame using Jython:

jython.png

>>> from javax.swing import JFrame # import the JFrame class
>>> j = JFrame(title='hello world!', size=(400,100)) # create a new JFrame instance and automatically sets title and size
>>> j.visible = 1 # the same as j.setVisible(1)

Notice that the 'new' ain't needed and you can access and modify attributes without get/set before. Also, integers can be used as booleans, but there are also available the True and False operators. The line setting the frame visible could also be inside the constructor, making the code even smaller.

It can be used to write automation scripts, including ones that use JDBC, use it as a script language inside your programs using the project jar or even use Jython compiler (jythonc) to transform the python code to Java code. All this using a very easy and high-level language.

I'm not trying to write a Jython tutorial here, this is just to give you a little taste. So, if you want more information about that, I recommend you to check this:

* http://www.jython.org/Project/index.html
* http://www.oreilly.com/catalog/jythoness/chapter/ch01.html
* http://www.onjava.com/pub/a/onjava/2002/03/27/jython.html

Related Topics >>