Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>I experienced a similar issue in that I want to be able to create simple command line calls for my jython apps, not require that the user go through the jython installation process, and be able to have the jython scripts append library dependencies at runtime to sys.path so as to include core java code.</p> <pre><code># append Java library elements to path sys.path.append(os.path.join(os.path.dirname(os.path.abspath(__file__)), "..", "..", "lib", "poi-3.8-20120326.jar")) </code></pre> <p>When running the 'jython' launcher explicitly on the command line, on Unix systems, it just runs a big shell script to properly form a java command line call. This jython launcher seems to have a dependency on reaching back to a core install of jython, and by some way of magic allows the proper handling of .jar files being added to the sys.path at runtime from within my .py scripts. You can see what the call is and block execution by the following:</p> <pre><code>jython --print run_form.py java -Xmx512m -Xss1024k -Dfile.encoding=UTF-8 -classpath /Applications/jython2.5.2/jython.jar: -Dpython.home=/Applications/jython2.5.2 -Dpython.executable=/Applications/jython2.5.2/bin/jython org.python.util.jython run_form.py </code></pre> <p>But it's still just firing up a JVM and running a class file. So my goal was to be able to make this java call to a standalone jython.jar present in my distribution's lib directory so users would not need to do any additional installation steps to start using my .py scripted utilities.</p> <pre><code>java -Xmx512m -Xss1024k -classpath ../../lib/jython.jar org.python.util.jython run_form.py </code></pre> <p>Trouble is that the behavior is enough different that I would get responses like this:</p> <pre><code> File "run_form.py", line 14, in &lt;module&gt; import xls_mgr File "/Users/test/Eclipse/workspace/test_code/py/test/xls_mgr.py", line 17, in &lt;module&gt; import org.apache.poi.hssf.extractor as xls_extractor ImportError: No module named apache </code></pre> <p>Now you might say that I should just add the jar files to the -classpath, which in fact I tried, but I would get the same result.</p> <p>The suggestion of bundling all of your .class files in a jython.jar did not sound appealing to me at all. It would be a mess and would bind the Java/Python hybrid application too tightly to the jython distribution. So that idea was not going to fly. Finally, after lots of searching, I ran across bug #1776 at jython.org, which has been listed as critical for a year and a half, but I don't see that the latest updates to jython incorporate a fix. Still, if you're having problems with having jython include your separate jar files, you should read this.</p> <p><a href="http://bugs.jython.org/issue1776" rel="nofollow">http://bugs.jython.org/issue1776</a></p> <p>In there, you will find the temporary workaround for this. In my case, I took the Apache POI jar file and unjar'ed it into its own separate lib directory and then modified the sys.path entry to point to the directory instead of the jar:</p> <pre><code>sys.path.append('/Users/test/Eclipse/workspace/test_code/lib/poi_lib') </code></pre> <p>Now, when I run jython by way of java, referencing my local jython.jar, the utility runs just peachy. Now I can create simple scripts or batch files to make a seamless command line experience for my .py utilities, which the user can run without any additional installation steps.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload