6

Jython standalone jar is throwing the ImportError exception at the time that I try to use Jython alongside Apache-POI.

Below you'll find how I call my Jython script:

java -cp C:\jAutoMailerScript\lib\poi-3.9-20121203.jar -jar jython.jar main.py

Error:

Traceback (most recent call last):

File "main.py", line 32, in

from org.apache.poi.hssf.usermodel import *

ImportError: No module named apache

This is the code at line#32:

from org.apache.poi.hssf.usermodel import *

Is there any restriction from Jython in order to work with Java's third-party applications?

Thanks in advance,

Eder
  • 1,874
  • 17
  • 34

4 Answers4

8

You cannot use -cp and -jar at the same time. The -jar option overrides any other class path settings. See http://docs.oracle.com/javase/7/docs/technotes/tools/windows/java.html.

Using the python.path system property should work:

java -Dpython.path=C:\jAutoMailerScript\lib\poi-3.9-20121203.jar -jar jython.jar main.py

Here is an alternative command:

java -cp C:\jAutoMailerScript\lib\poi-3.9-20121203.jar;jython.jar org.python.util.jython main.py

However, it turns out that none of these commands work with standalone Jython. You get an ImportError, just as it says in the question. There is an old open bug that seems relevant: http://bugs.jython.org/issue1422 (it says that the problem exists on Solaris, but it does apply to other platforms too as far as I can tell).

Using installed Jython and the jython command works fine:

jython -Dpython.path=C:\jAutoMailerScript\lib\poi-3.9-20121203.jar main.py

Note that the standalone jython.jar includes the standard library Python modules (in the Lib folder). These modules are not included in the jython.jar that you get with installed Jython.

I hope that this answer helps, even if it may not solve your problem completely.

mzjn
  • 48,958
  • 13
  • 128
  • 248
  • It's still throwing the exception message: ImportError. – Eder Mar 05 '13 at 00:56
  • But what would happen if I want to release this for it to being used on another computer? Would it be possible to pack it? – Eder Mar 05 '13 at 16:41
  • The "basic install" jython.jar does not include the standard library Python files that are bundled in the standalone jython.jar. That needs to be considered of course. I'm not sure what the best solution is in your case. – mzjn Mar 05 '13 at 18:58
3

I've been trying to reproduce your problem and encountered the same with the 2.5.3 version of standalone Jython. Also tried with POI 3.7; still the same deal. I also tried the sys.path.append suggestion, Arshad made. Something peculiar is happening here (testing with a different library - this time barcode4j):

c:\development\local\lib\jython-sa-2.5.3>java -jar jython-standalone-2.5.3.jar
Jython 2.5.3 (2.5:c56500f08d34+, Aug 13 2012, 14:54:35)
[Java HotSpot(TM) 64-Bit Server VM (Oracle Corporation)] on java1.7.0_10
Type "help", "copyright", "credits" or "license" for more information.
>>> import sys
>>> sys.path
['', 'C:\\development\\local\\lib\\jython-sa-2.5.3\\Lib', 'C:\\development\\local\\lib\\jython-sa-2.5.3\\jython-standalone-2.5.3.jar\\Lib', '__classpath__', '__pyclasspath__/']
>>> sys.path.append('C:\development\local\lib\barcode4j-2.0\build\barcode4j.jar')
>>> sys.path
['', 'C:\\development\\local\\lib\\jython-sa-2.5.3\\Lib', 'C:\\development\\local\\lib\\jython-sa-2.5.3\\jython-standalone-2.5.3.jar\\Lib', '__classpath__', '__pyclasspath__/', 'C:\\development\\local\\lib\x08arcode4j-2.0\x08uild\x08arcode4j.jar']
>>>

See how the path gets scrambled because of the '\' delimiters?

(Also tried the python.path suggestion but it gives the same error as you reported.)

Could it be you're running into this reported issue or something similar/related? It does seem to match the scenario (standalone version) and the versions you and I used.

Wivani
  • 2,036
  • 22
  • 28
3

Same issue with jython 2.5.4-rc1 standalone using commons-lang3-3.1.jar, etc. I have to use it in Standalone mode so this is very frustrating! :-(

EDIT: This person figured it out! Why does Jython refuse to find my Java package?

You must have to add the following flags for Jython standalone to work!

java -Dpython.cachedir.skip=false -Dpython.cachedir=/tmp {...}
Community
  • 1
  • 1
elec3647
  • 513
  • 5
  • 8
1

You can try to append the jar to your system path like this

sys.path.append('C:\jAutoMailerScript\lib\poi-3.9-20121203.jar')

And then try to run the same script. Although it would be better to use os module to get to the path. I'm not sure how the slashes are treated in jython on windows OS.

Arshad Ansari
  • 324
  • 4
  • 10
  • @Arshard_Ansari: It is still not working... has this anything to do with the jython version that I'm using? (Jython Standalone 2.7b) – Eder Feb 26 '13 at 02:07