vgel.me

- projects & stuff

permalink for Packaging Jython scripts in a single executable Jarfile

Packaging Jython scripts in a single executable Jarfile

There's a lot of outdated and just plain wrong information on the internet about packaging Jython scripts. After I wanted to start a project in Jython, I figured I should find a simple, easy way to package your app as an executable (ie, double-clickable) .jar. Turns out, it's a lot easier than it seems.

The only caveat to this process seems to be to use a relatively-recent Jython version. Some poking around suggests >2.5.1 should work, I used 2.5.3.

First, download jython-standalone, and copy it to your project directory as jython.jar. Next, create the file manifest.txt with the following content:

    Main-Class: org.python.util.JarRunner

Now, create __run__.py with the following content:

    import app

And create the folder app. You can place any additional startup code in either __run.py__ or app/__init__.py, along with placing your other code files in app. Now, code the same as you would any other Python project (along with all that Jython goodness). When you want to build your project, create a shell script with this:

#!/bin/sh
rm output.jar 2>/dev/null
cp jython.jar output.jar
zip -r output.jar app
zip output.jar __run__.py
jar ufm output.jar manifest.txt
rm jartmp*.tmp 2>/dev/null
echo "Done building!"

Which will package your app as output.jar. Now, you can run java -jar output.jar, or double-click output.jar. Enjoy!