Desi Programmer @ Work

Secret Weapons for Java Programmers

Many great hackers and programmers do not consider Java programming language a good choice. The safety features of Java such as pure OO, strong typing etc. force its users to laboriously write lots of classes, declare and typecast variables, call extraneous methods to fetch simple data and puts under a lot of restrictions. Such techniques might be good to prevent a mediocre coder from making blunders and protecting him from shooting himself in the foot. However for great hackers these things only take away the freedom to freely convert their thought process into code and make effective use of their time. The extra safeties are considered distractions which force the users to focus on implementation rather than the logic.However it is a fact that many good programmers do use Java as their main tool. Some of them use it because they’ve never been exposed to great stuff such as Lisp, Ruby or Python. Others are those who have been forced to use it by their Pointy-Haired-Bosses or unavailability of a good library.I want to help out both of these types of good programmers. Recently I went through a similar situation where I could not find a library except in Java. I thought being forced to Java is going to be a nightmare for me but I found a fantastic work around which lets you use power of great languages while staying in the Java platform.I wanted to use a good library for interfacing with my GSM Modem. The only reasonable library I could find was SMSLib. Bad day: it was written in Java. I tried to play around with it for a while by the standard hit & trial methods of writing small code snippets with many printf’s and compiling and running to see the result. But I was soon lost in memorizing dozens of classes, pointless getters and setters and other irrelevant stuff I did not need at all to implement my logic.Before banging my head against the wall and rewriting the whole library in C with interfaces to Python and Lisp, I decided to give a try to one of the scripting languages for Java platform. I’d heard about Jython, Rhino, Grooby and JRuby. Since my favorite language is Python, I chose Jython for the experiment.I downloaded Jython from Its installation was a charm. Double clicking the downloaded JAR did everything for me including writing a batch file for setting up environment variables. Running the batch files gives Jython’s shell. The shell is exactly like Python’s shell except that it runs in Java and can run Python code as well as uses Java’s libraries and classes.

C:/> Jython
Jython 2.2.1 on java1.6.0_01Type "copyright", "credits" or "license" for more information.

First thing to do for any programming experiment: Print Hello World. How do I do that?

>>> print Hello World
Hello World

What? Don’t I have to make a separate class with a static void type main method which instantiates printer factory, inherited by abstract factory to take a message as argument and forward it to system.out.println and use that factory to print “Hello World”? No! Because you are still using Java’s platform, but on top of it instead of Java language you are using one of the most powerful languages.Before moving on I just wanted to make sure if I still had the power of Python. So I tried writing a MD5 reverser in three lines which takes a file containing list of commonly used passwords.

>>> from md5 import md5
>>> f = open('cpasses.txt')
>>> p = dict([ (md5(s.strip()).hexdigest(), s.strip()) for s in f.readlines()])
>>> p['f30aa7a662c728b7407c54ae6bfd27d1'] # Testhello123

Beautiful! Lets try importing Java classes.

>>> from java.util import *>>> dir()['AbstractCollection', 'AbstractList', . 'regex', 'spi', 'zip']

Cool! I can now use all classes of Java with Python’s syntax. Let’s try SMSLib with Jython.

>>> from org.smslib import *Traceback (innermost last):File "", line 1, in ?ImportError: no module named smslib

All I had to do was put SMSLib’s Jar in CLASSPATH.

>>> from org.smslib import *
>>> dir()
['AGateway', 'AsyncEvents', 'DeliveryStatuses'  'smsserver', 'smssvr']
>>> c = Service()
>>> dir ( c )
['S', '__init__', 'addGateway', 'class', 'deleteMessage'  'toString', 'wait']

You can introspect methods while you are experimenting giving commands. This effectively means that you can debug, code and execute all at the same time. In the interactive mode the state of execution is maintained as you give command so you can try out small things without regenerating the whole scenario!By trying out small things I was able to easily figure out how to use different stuff of SmsLib. Afterwards I copy pasted all the relevant commands I executed in a separate .jy file and made small changes to carve out my required code. Jython made my day!Not only just Python, but you can also use Ruby and Javascript over Java platform using JRuby and Rhino. There is also Groovy, another dynamic language specifically designed for Java. These languages make programming with Java platform much more powerful. By power, I mean the ability to code conveniently and timely without getting frustrated.So the next time your Pointy-Haired-Boss tells you that the next project is to be done in Java, don’t argue; just include an extra JAR of your favorite powerful language in your Java project.PS: Python and Ruby are also available for .net but I haven’t tried them out.