Jython, building a project that uses jars, and thoughts on better tool

Quick and Dirty and Works!

So if you are reading this, and using Jython, you’ll want to use Java jars I’ll wager. Jython’s major selling point for me was being able to leverage a ton of libraries that I’ve gotten used to while working in the Java-sphere. This is pretty simple. When Jython boots up it looks for a variable named CLASSPATH, which you’ll configure in your Bash shell. What I’ve done in most projects up to now is create a folder named lib, although perhaps java_lib would be a better name, and drop all the jars I’ll need in there. Then I usually whip up a script like this:


for e in lib/*.jar; do

echo "Adding jars: $CLASSPATH"


Which when run from the project’s folder will properly setup the variable. Now you can run Jython and be able to import and run libraries from your jars.

Could We Do this Better?

Geez I sure hope so. One other need that’s not fully addressed by any Jython tool ¬†yet (that I know of) is jar dependency management. It’s easy to create Python projects that are aware of the libraries they’ll need to run; most of you have to have run into a setup.py file and installed libraries using it. Well if your project works by using a number of Python and Java libs, how do you suppose that you’ll install the Java libs? Of course we’ve already talked about just including them all in your project in a ‘lib’ folder. But this isn’t really the best option for anything but the simplest projects. For one, your SVN/HG/GIT will be bloated by the actual libraries being included. It is also quite easy to end up with various copies of the same jar on your machine, and the confusion of managing them all in such a hands on way.

One way we can deal with this issue today is by using the Maven tool. Right away I should say this’ll complicate the install process, since you’ll have both setup.py and pom.xml files describing Python and Java libs respectively. Then also note you’ll have to run two command to get both Python and Java libraries installed. As far as I know though it’s one of the only ways to go toolwise currently. I won’t go into the technical details of using Maven here, mostly because it’s probably not a good idea if you are a proper Python programmer to learn Maven. It’s convoluted at best, and really a beast of Java bureaucracy.

I think, if it’s possible maybe the community could whip up a little plug-in to easy_install or pip or both (does either of them allow plugins?) The convention used by the Java dep management tools to figure out how to obtain a jar if pretty basic. You have a repository, and in that you have a simple file system for getting a jar you want. Most Python people are only used to downloading their libs from Pypi (a lib repository), but in the Maven world it’s much more intense and you can have lot’s of repos (and by golly there are!) So what would work best, in my opinion, is to be able to alter a setup.py and include references to Java jars. First thing that is needed is a way to spec the repositories in setup.py, with a name/url pair. Then there is the dependencies themselves. With setup.py we’re used to just having a name, and a version. In Java it’s a group name, a artifact name (same as the setup.py name) and a version. We would need to way to add in this information, and also to set a 1 to 1 with a repository. So once you have the location of a repo (URL) you can get a jar by using the following path template


Also useful might be the information that the “artifact-version.jar” could be “artifact-version-sources.jar” for the jar containing all the sources for the library. This isn’t always available but if it is it’ll probably we worth grabbing.

So that’s about all there is to it. We could update the tools to be Jython sensitive and allow for spec’ing these that’d be a big step in building more professional Jython projects.

2 thoughts on “Jython, building a project that uses jars, and thoughts on better tool

  1. If you are using maven this may do the trick and save you from having a lib folder in your code repo. Adding every jar in your maven repository does seem like a bit of a shotgun approach.



    CLASSPATH=`find $M2_PATH -name "*.jar" | tr "\\n" ":"`

    $JYTHON -Dpython.path=$CLASSPATH $1

    1. Hi Russ,

      I think this’ll add everything in a Maven repo to the classpath, no? That’s fine and does the job but what I’d like to see is a less than brute force approach.

      A well designed tool would allow for creating packaged applications that use Jython with respect to jars. Being able to easily use Maven to create a project and package it as a war would be the optimum.

      I’d like to see a way to do both Django and Pylons/Pyramid projects this way. Run one Maven command to generate a Pylons project, then you add the libs you need in the setup.py like normal, but then Maven install the proper jars and generate the war file for deployment.

      BTW I give you mad props for the use of “tr”!

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>