Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Standalone Jython: Import Error (Apache-POI)

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,

like image 456
Eder Avatar asked Feb 22 '13 02:02

Eder


3 Answers

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.

like image 180
mzjn Avatar answered Nov 15 '22 09:11

mzjn


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.

like image 22
Wivani Avatar answered Nov 15 '22 08:11

Wivani


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 {...}
like image 3
elec3647 Avatar answered Nov 15 '22 08:11

elec3647