无法从 Jython 模块导入 Java 类

2022-09-04 22:34:13

注意:我找到了解决方案并回答了自己。但是,我不知道为什么该设置是错误的并导致问题。我仍然有兴趣对Jython导入系统的工作原理进行很好的解释;如果有人想获得赏金,请回答这个问题。


我正在开发一个现有的Java EE项目,我需要用Python进行计算。我正处于集成测试的第一阶段,但我已经遇到了一个问题。我读了Jython书的第10章,但仍然找不到解决方案。我还阅读了第 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

导入由私有 Jython 注册表文件定义的路径:

$ 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

建筑类型.java

package pendulum.generator;

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

答案 1

你需要设置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

参考:使用CLASSPATH(Jython Book)。


答案 2

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

.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