Packaging a Jython program in an executable jar
Asked 07 September, 2021
Viewed 454 times
  • 66
Votes

I am trying to package a jython program into an executable jar which the user can simply double-click to run without installing jython ahead of time. Ultimately, I would like to include an additional library which I wrote with the jar, but at the moment I am just trying to package a simple program and have it run from the jar.

I have tried following the jar instructions in the "Using the Jar Method" section here: Jython FAQ: Using the Jar Method

I have also looked at slides 25-28 here: Jython Update 2012 slides

And finally here: stackoverflow Question: Distributing My Python Scripts as Jars with Jython

I have installed jython 2.5.3, jvm 1.6, and python 2.7.3 on my mac which is running OS X 10.8.3.

These are the steps I go through to create the jar and run it:

  1. Create a copy of jython.jar from my jython installation directory.
  2. zip -r jython_copy.jar Lib (where Lib is the folder in the jython installation directory)
  3. cp myJythonProgram.py __run__.py (myJythonProgram.py does not include an 'if name == main' line)
  4. zip jython_copy.jar __run__.py
  5. export CLASSPATH=/path/to/my/app/jython_copy.jar:$CLASSPATH

I have tried running the jar using all three of these methods:

  1. java org.python.util.jython -jar myapp.jar
  2. java -cp myapp.jar org.python.util.jython -jar myapp.jar
  3. java -jar myapp.jar -jar myapp.jar

This works if my program doesn't use any import statements.

However I am running into an issue where some python packages are not able to be found when I run the jar. For instance, I get the error "ImportError: No module named random" when I include the line from random import random in my program. No errors occur on lines in the program when I import from javax.swing, java.awt, time, or math.

Additionally, I tried to package a jar with my library and a jython program which imports my library using the previous steps as well as the following additional steps:

  1. zip jython_copy.jar myLibrary.jar
  2. jar ufm jython_copy.jar othermanifest.mf

othermanifest.mf only contains the line Class-Path: ./myLibrary.jar.

This too gives the error "ImportError: No module named myLibrary"

I would appreciate any insight into what I am doing incorrectly or other steps I should take.

Thanks!

2 Answer