2013-02-14 207 views
13

注:我找到了解決方案並自己回答。雖然,我不知道爲什麼這種設置是錯誤的並導致了問題。我仍然對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(); 
} 
+0

嘗試「從鐘擺。生成器導入BuildingType「 – sarwar 2013-02-14 16:43:22

+0

ImportError:無法導入名稱BuildingType – Paolo 2013-02-14 16:45:22

+1

jython.jar是否在jre/lib/ext? – 2013-02-15 17:41:31

回答

4

在try/catch方法浪費了很多時間之後,我能夠自己找到答案。

.jython 看起來像這樣:

python.path=\ 
/path/to/project/build:\ 
/path/to/project/src:\ 
/home/me/jdevel/extras/2.5.3/Lib:\ 
/home/me/jdevel/extras/2.5.3/Lib/site-packages 

不是這個:

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 

尤其加入/path/to/project/build/classes的導入路徑是錯誤(即使它反映了文件系統層次結構),而/path/to/project/build是正確的,並解決了這個問題。

4

您需要設置CLASSPATH以及python.path。

在相同的目錄佈局,這個工作對我來說:

jython10$ CLASSPATH=build/classes/ jython -v Building.py 
import: 'exceptions' as org.python.core.exceptions in builtin modules 
import: import site # precompiled from /usr/local/Java/jython2.5.3/Lib/site$py.class 
import: 'sys' as sys in builtin modules 
import: import os # precompiled from /usr/local/Java/jython2.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 /usr/local/Java/jython2.5.3/Lib/posixpath$py.class 
import: import stat # precompiled from /usr/local/Java/jython2.5.3/Lib/stat$py.class 
import: java package as '/Users/sdm7g/jaxp/jython10/build/classes/pendulum' 
import: 'pendulum' as java package 
import: 'pendulum' as java package 
import: java package as '/Users/sdm7g/jaxp/jython10/build/classes/pendulum/generator' 
import: 'pendulum.generator' as java package 
import: 'BuildingType' as java class 

參考:Working with CLASSPATH(Jython的書)。

+0

非常感謝。沒有設置classpath,我無法從命令行使用我的類('java.lang.ClassNotFoundException')。儘管如此,從Eclipse內部一切正常。 – Paolo 2013-02-22 21:13:15