无法从Jython模块导入Java类

时间:2013-02-14 16:18:39

标签: java python import jython

注意:我找到了解决方案并自行回答。虽然,我不知道为什么这个设置错了并导致了问题。我仍然对Jython导入系统如何工作有一个很好的解释感兴趣;如果有人关心获得赏金,请回答。


我正在开发一个现有的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中,但我不知道哪个值适合。


代码组织:

$ 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

private Jython registry file定义的导入路径:

$ 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会选择路径,因为我在Jython提示符下用sys.path检查了它。

BuildingType.java

package pendulum.generator;

public interface BuildingType {
    public String getBuildingName();
    public String getBuildingAddress();
    public String getBuildingId();
}

2 个答案:

答案 0 :(得分: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 Book)。

答案 1 :(得分:4)

在尝试/捕捉方法浪费了大量时间之后,我能够自己找到答案。

.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是正确的并解决了问题。