This article will show you some basic steps to setup a standalone environment for Jython, with the power of some popular and standard tools.
0. Install essential tools
Take Ubuntu Maverick as an example, before starting, you have to make sure these packages installed on your system.
Essential Python tools (Python is shipped with most Linux distribution by default)
sudo apt-get install python-pip python-virtualenv
sudo apt-get install openjdk-6-jdk openjdk-6-jre
The Jython package in Ubuntu repository is obsolete. Download Jython installer from official website. A full installation is recommended here.
As an optional step, I often move Jython direcotry to /usr/local/share/ , and use a symbol link to add Jython to PATH:
sudo ln -s /usr/local/share/jython/bin/jython /usr/local/bin/
Install additional Java tools
sudo apt-get install ant ivy
1. Create a standalone environment
Jython 2.5 is fully compatible with virtualenv, so you can create a standalone environment just like what you do with python:
virtualenv -p /usr/local/bin/jython jython-env
List the directory, you will see these files and directories:
Source the activate script:
2. Download and install python packages
Now the python environment is just ready, you can install python packages as you want. Install bottle, for example:
pip install bottle
3. Download and install Java dependencies
This is the different step. As you know, the most important feature of Jython is availability of Java libraries to Python code. So we need some additional tools to manage Java dependencies. Popular Java lifecycle management tool Maven is not suitable here, because Maven is tightly depends on its archetype. Gradle is also known as a powerful build tool with DSL support. However, Gradle uses Groovy as the scripting language to create build file. Bringing Groovy to a Jython project seems terribly strange.
So I prefer the traditional Ant way to manage dependencies. First of all, create a build file with following content:
<target name="resolve" description="retrieve dependencies">
<ivy:retrieve pattern="javalib/[artifact]-[revision].[ext]" type="jar"/>
Then create an ivy xml to configure dependencies:
<info organisation="info.sunng" module="jython-env" />
<dependency org="commons-lang" name="commons-lang" rev="2.5"/>
Also, as an optional step, you can overwrite default settings to configure ivy to use maven local repository.
<chain name="maven-or-not" returnFirst="true">
<filesystem name="maven-local" m2compatible="true" />
<ibiblio name="ibiblio" m2compatible="true" />
All done, now you can download everything. In Ubuntu, default ivy installation, you have to specify ivy path in ant command line:
ant -lib /usr/share/java/ivy.jar
You will see jars in javalib.
4. Reconfigure start up script
The default Jython start up script is not friendly to external Java dependencies. To include jars, you have to use such dirty command line parameters:
To get rid of this, first rename bin/jython to bin/startJython. (Follow the Groovy naming convention)
Then create a new bin/jython script wraps the old one:
for lib in $JAVA_LIBS
Finally, it comes to an end. Type jython to execute the interactive shell:
Jython 2.5.1 (Release_2_5_1:6813, Sep 26 2009, 13:47:54)
[OpenJDK Server VM (Sun Microsystems Inc.)] on java1.6.0_20
Type “help”, “copyright”, “credits” or “license” for more information.
>>> from org.apache.commons.lang import BitField
>>> import bottle
Both Python and Java dependency is accessible. Now it’s time to get your new start.