只需添加您的罐子到sys.path中,像這樣:
~ $ jython
Jython 2.5.0+ (trunk:6691, Aug 17 2009, 17:09:38)
[Java HotSpot(TM) Client VM (Apple Computer, Inc.)] on java1.6.0-dp
Type "help", "copyright", "credits" or "license" for more information.
>>> from org.thobe.somepackage import SomeClass # not possible to import yet
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: No module named thobe
>>> import sys
>>> sys.path.append("/var/javalib/some-thobe-package.jar") # add the jar to your path
>>> from org.thobe.somepackage import SomeClass # it's now possible to import the package
>>> some_object = SomeClass() # You can now use your java class
它不能得到比:)
在你的情況,你可能想使用的路徑,更簡單的包找到罐子:
# yourpackage/__init__.py
import sys, os
if 'java' in sys.platform.lower():
sys.path.append(os.path.join(os.path.dirname(os.path.abspath(__file__)),
"your-lib.jar"))
from jython_implementation import library
else:
from cpython_implementation import library
希望有所幫助!
thx提醒我jython有多棒。 – AndreasT 2011-09-23 08:30:43
不清楚「來自org.thobe.somepackage import SomeClass」的方式是基於包「some-thobe-package」的名稱。我收集到你需要「來自組織」。但是「some-thobe-package.jar」在導入時如何轉換爲「thobe.somepackage」? – 2012-02-12 09:50:27
@delinquentme由於jar文件位於路徑中,因此其內部結構就是引用的內容。如果要運行'jar -xvf/var/javalib/some-thobe-package.jar'你會發現這個結構是org/thobe/somepackage/SomeClass.class – Eric 2012-03-13 19:09:13