Java and Scripting

First let me outline the dream:

I need to add scripting functionality to one of my projects, i.e. my GUI will have a textArea where the user can write some simple code to customize my software’s functionality. My project needs to be multi-platform. I hope to ship all the required libraries/dependencies together so that it just works, provided the user has the Java Runtime Environment (JRE) installed.

Now let me outline the baseline reality:

Python is probably the most prolific scripting language around these days. It is super-supported, super-functional, super-easy-to-read (cf perl), super-free. If I’m going to force my users to learn a scripting language, it might as well be Python. The only reasons I’m not happy to stop my search right here is that communication between my Java code and their Python scripts would be messy, involving environmental variables or a formatted file. Futhermore, I would be asking my users to have not just the JRE installed, but also the Python interpreter.

So is the dream shattered? Maybe not. I’m hoping to find a script interpreter written as a JAR which I can ship with my project. Is this what Jython is?

… to be continued.

– 1 day later –

Indeed Jython is the answer. The Jython installer is a Java Program (jar) has a few installation options. The default install will let you run Jython from the command line terminal. I haven’t tried that one, but assume it is for those who want to build Java programs (and have access to Java libraries), but do their coding in Python instead of Java. That’s not what I want. It turns out I wanted a “standalone” installation. The standalone installation simply produces a Java library (called jython.jar) containing the Python/Jython interpreter. If you include this jar in your Java project’s classpath, then you can instantiate a jython interpreter inside your Java program and pass it strings of python script (which come from say a JTextArea). When exporting the Java application, Eclipse gives you the choice of rolling all dependencies (which will include jython.jar) into your project’s exported jar. This is very tidy, but it may violate the licences of some libraries, so a safer practice is to let Eclipse add the required jars, unmodified, in your export directory.

Example program: TheMulligan

My test program above, “TheMulligan” is a home made GUI for the Jython interpreter. It has two side-by-side JTextAreas. The left one is for Python script input, and the right one is the OutputStream for the jython interpreter. Script typed into the left JTextArea can be run, and the JTextAreas can be cleared. (Ok, so its a bit crude… and there is no error catching… but hey, it is proof of concept.)

The relevant bits of code for this blog post involve creating a new JPythonInterpreter object

jpinterp = new PythonInterpreter(); jpinterp.setOut(new taOutputStream());

and later using it to execute the text in the textArea (this method is triggered by a button press)

public void actionPerformed(ActionEvent arg0) throws PyException { jpinterp.exec(textArea.getText()); } 

Notice that all of this is still completely platform indepependent. Your project can be launched from any machine running the JRE, and doesn’t require any local python/jython. This is me, living the dream =)