问题
I am trying to use java's WebClient jar within a jython script.
I am running a jython script like so:
jython -Dpython.path=/home/tipu/Dropbox/dev/proj/lib/* test.py
the contents of test.py:
import com.gargoylesoftware.htmlunit.WebClient as WebClient
def main():
webclient = WebClient() # creating a new webclient object.
if __name__ == '__main__':
main()
The error I get is:
Traceback (innermost last):
File "scraper.py", line 1, in ?
ImportError: no module named gargoylesoftware
This is the conents of the lib folder in the python.path directory:
commons-codec-1.4.jar commons-lang-2.6.jar htmlunit-2.9.jar httpcore-4.1.2.jar sac-1.3.jar xercesImpl-2.9.1.jar
commons-collections-3.2.1.jar commons-logging-1.1.1.jar htmlunit-core-js-2.9.jar httpmime-4.1.2.jar serializer-2.7.1.jar xml-apis-1.3.04.jar
commons-io-2.0.1.jar cssparser-0.9.5.jar httpclient-4.1.2.jar nekohtml-1.9.15.jar xalan-2.7.1.jar
Any idea as to why it isn't working?
回答1:
I don't think wildcards work when setting a system property like python.path
. Try this:
$ CLASSPATH=/home/tipu/Dropbox/dev/proj/lib/*
$ jython test.py
回答2:
what you need to do is adding the .jar files to java's classpath. This is the location where java looks for modules/classes to be imported. In Mac OS: open up a terminal, and enter:
export CLASSPATH=$CLASSPATH:/x/y/htmlunit-2.11/lib/*
where /x/y/htmlunit-2.11/lib/ is the directory where all the .jar files are located.
来源:https://stackoverflow.com/questions/8774498/instantiating-a-webclient-object-in-jython-giving-strange-results