注:我找到了解決方案並自己回答。雖然,我不知道爲什麼這種設置是錯誤的並導致了問題。我仍然對Jython導入系統的工作原理有很好的解釋。如果有人關心獲得賞金,請回答。無法從Jython模塊導入Java類
我正在研究現有的Java EE項目,我需要在Python中進行計算。我處於集成測試的第一階段,但我已經面臨一個問題。我讀Chapter 10 of Jython book但仍然找不到解決方案。我也讀Chapter 8(代碼重用的模塊和包),但對我來說目前還不清楚。
Jython導入系統如何工作以及如何配置它的說明將非常感謝。
問題:
$ jython -v
import: 'exceptions' as org.python.core.exceptions in builtin modules
Jython 2.5.3 (2.5:c56500f08d34+, Aug 13 2012, 14:54:35)
[Java HotSpot(TM) Server VM (Oracle Corporation)] on java1.7.0_10
import: import site # precompiled from /home/me/jython/2.5.3/Lib/site$py.class
import: 'sys' as sys in builtin modules
import: import os # precompiled from /home/me/jython/2.5.3/Lib/os$py.class
import: 'errno' as org.python.modules.errno in builtin modules
import: 'posix' as org.python.modules.posix.PosixModule in builtin modules
import: import posixpath # precompiled from /home/me/jython/2.5.3/Lib/posixpath$py.class
import: import stat # precompiled from /home/me/jython/2.5.3/Lib/stat$py.class
import: 'java' as java package
import: 'File' as java class
import: 'IOException' as java class
import: 'org' as java package
import: 'Py' as java class
Type "help", "copyright", "credits" or "license" for more information.
>>> import pendulum.generator.BuildingType
import: import pendulum # precompiled from /path/to/project/build/classes/pendulum/__init__$py.class
import: import pendulum.generator # precompiled from /path/to/project/build/classes/pendulum/generator/__init__$py.class
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: No module named BuildingType
我的問題是:什麼我做錯了,我怎麼解決的事情,成功導入BuildingType接口?也許問題在於CLASSPATH,但我不知道哪個值可能適合。
代碼組織:由private Jython registry file定義
$ tree build/classes/pendulum/generator/ src/pendulum/generator/
build/classes/pendulum/generator/
├── BuildingType.class
├── __init__.py
└── __init__$py.class
src/pendulum/generator/
├── BuildingType.java
└── __init__.py
導入路徑:
$ cat ~/.jython
python.path=\
/path/to/project/build/classes:\
/path/to/project/src:\
/home/me/jdevel/extras/2.5.3/Lib:\
/home/me/jdevel/extras/2.5.3/Lib/site-packages
我敢肯定的Jython拿起路徑,因爲我檢查與sys.path
在Jython提示符下。
BuildingType.java
package pendulum.generator;
public interface BuildingType {
public String getBuildingName();
public String getBuildingAddress();
public String getBuildingId();
}
嘗試「從鐘擺。生成器導入BuildingType「 – sarwar 2013-02-14 16:43:22
ImportError:無法導入名稱BuildingType – Paolo 2013-02-14 16:45:22
jython.jar是否在jre/lib/ext? – 2013-02-15 17:41:31