- 装备 tags:
- ant
- java
- jython
- python published: true comments: true
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
Java sudo apt-get install openjdk-6-jdk openjdk-6-jre
Jython The Jython package in Ubuntu repository is obsolete. Download Jython installer from official website. A full installation is recommended here. http://jython.org/downloads.html
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 cd jython-envList the directory, you will see these files and directories:
- bin/
- cachedir/
- Lib/
- jython.jar
- registry
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 bottle3. 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:
build.xml
[cc lang="xml"]
Then create an ivy xml to configure dependencies:
ivy.xml
[cc lang="xml"]
Also, as an optional step, you can overwrite default settings to configure ivy to use maven local repository.
ivysettings.xml
[cc lang="xml"]
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: jython -Dpython.path=javalib/commons-lang-2.5.jar:javalib/...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:
[cc lang="bash"]
#! /bin/bash
JYTHON_CMD="startJython"
JAVA_LIBS="javalib/*.jar"
PYTHON_PATH=""
for lib in $JAVA_LIBS
do
PYTHON_PATH="$PYTHON_PATH:$lib"
done
#echo $PYTHON_PATH
$JYTHON_CMD -Dpython.path=$PYTHON_PATH
[/cc]
5. Run
Finally, it comes to an end. Type jython to execute the interactive shell: jython[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.