Standalone Jython: Import Error (Apache-POI)

左心房为你撑大大i 提交于 2019-12-07 04:53:07

问题


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,


回答1:


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.




回答2:


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.




回答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 {...}



回答4:


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.



来源:https://stackoverflow.com/questions/15016039/standalone-jython-import-error-apache-poi

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!