可以將Jython腳本運行爲:jython myscript.py如果腳本使用腳本需要運行的jar的sys.path.append將完整url附加到python路徑。
這是我正在處理的當前腳本。
#!/opt/jython/jython
'''
Created on Dec 7, 2011
@author: chris
'''
import sys, os
from time import sleep
jarpath = '/usr/share/java/htmlunit/' #path the jar files to import
jars = ['apache-mime4j-0.6.jar','commons-codec-1.4.jar',
'commons-collections-3.2.1.jar','commons-io-1.4.jar',
'commons-lang-2.4.jar','commons-logging-1.1.1.jar',
'cssparser-0.9.5.jar','htmlunit-2.8.jar',
'htmlunit-core-js-2.8.jar','httpclient-4.0.1.jar',
'httpcore-4.0.1.jar','httpmime-4.0.1.jar',
'nekohtml-1.9.14.jar','sac-1.3.jar',
'serializer-2.7.1.jar','xalan-2.7.1.jar',
'xercesImpl-2.9.1.jar','xml-apis-1.3.04.jar'] #a list of jars
def loadjars(): #appends jars to jython path
for jar in jars:
print(jarpath+jar+'\n')
container = jarpath+jar
sys.path.append(container)
loadjars()
import com.gargoylesoftware.htmlunit.WebClient as WebClient
webclient = WebClient()
def gotopage():
print('hello, I will visit Google')
url = 'http://google.com'
page = webclient.getPage(url)
print(page)
if __name__ == "__main__":
gotopage()
嗨,你能夠運行該程序? –