Skip to content

jython niceties

While playing around with the Java JDOM library, I found myself resorting to jython to experiment with the API. It’s just so much easier this way for me:

#!/usr/bin/env jython
 
from java.io import StringReader
 
from org.jdom import Document
from org.jdom.input import SAXBuilder
from org.jdom.xpath import XPath
 
xml = '<foo><bar>foobar</bar></foo>'
 
builder = SAXBuilder()
document = builder.build(StringReader(xml))
xpath = XPath.newInstance('//foo/bar')
node = xpath.selectSingleNode(document)
print node.getText()

In case it’s of interest I’ve got a little jython startup script which automatically makes .jar files I drop in a particular directory available to the interpreter. So when testing jdom all i had to do was drop jdom.jar in my /usr/local/jython/jars and it’s immediately available the next time I start up jython.

#!/bin/bash
 
JYTHON_HOME=/usr/local/jython
 
for jar in $JYTHON_HOME/jars/*.jar
do
    jars=&quot;$jars:$jar&quot;
done
 
CLASSPATH=&quot;$JYTHON_HOME/dist/jython.jar$jars&quot;
 
java -cp $CLASSPATH \
	$JYTHON_JAVA_ARGS -Dpython.home=$JYTHON_HOME \
	org.python.util.jython &quot;$@&quot;

Pretty handy, especially for interactive sessions.

Post a Comment

You must be logged in to post a comment.