Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Packaging a Jython program in an executable jar

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!

like image 853
Kyle Avatar asked May 22 '13 21:05

Kyle


People also ask

What is a JAR file in Python?

jar file is a compiled collection of classes that can be dropped into your application, or installed anywhere on your CLASSPATH . In Python: A . py (or . pyc ) module can be dropped into your application, or installed anywhere on your sys.


2 Answers

I realized what the problem was and I wanted to document it in case anyone else has the same problems.

I was using the jython.jar file that came in the standard installation of Jython, and NOT the standalone jython.jar (the instructions at Using the Jar Method mentions this, but the instructions at Building Jars do not). I am still unsure why copying the Lib/ folder of the standard installation into the jython.jar that came with that installation didn't work on my system. However, once I used the standalone jar, things started to work more smoothly.

Additionally, I was able to get my library to work with the packaged file by doing three things in addition to the steps I laid out in my question:

  1. Exploding the standalone jython.jar and copying the folder with all of my library files into Lib, then create a new jar. This seemed to be the easiest way to include my library and allows me to package everything into a single jar.

  2. I discovered after reading Frank Wierzbicki's answer in Why does Jython refuse to find my Java package? that because I am now using the standalone jar, I could no longer use imports of the style from java.awt import *, instead I needed to fully specify each thing I was importing, for example from java.awt.Font import PLAIN, BOLD, ITALIC. So I went through the library's imports and fixed the few that were of the wrong style.

  3. Now that I am adding my Library directly to the Jar's Lib folder, instead of writing Class-Path: ./myLibrary.jar in othermanifest.mf, I put Main-Class: org.python.util.JarRunner as per Frank Wierzbicki's answer in the post I mentioned in my question: Distributing my Python scripts as JAR files with Jython?

This allowed me to create a double-clickable executable jar containing my library and jython file that I wanted to run.

like image 184
Kyle Avatar answered Jan 01 '23 23:01

Kyle


There are two solutions. They both work, but one better than the other.

I believe you can rename your python script as __run__.py, place that file inside the .jar file, and pass the .jar file through a python interpreter. See https://wiki.python.org/jython/UserGuide#invoking-the-jython-interpreter for more.

Multiple methods to run Jython from the java code while running through JVM are described here, at the Jython documentation.

EDIT:

You can execute a command line code that runs the python file you want. Link to an example of running command line code from java here.

like image 21
W. Conrad Walden Avatar answered Jan 01 '23 23:01

W. Conrad Walden