【发布时间】:2013-04-04 06:02:08
【问题描述】:
我在 Python 中使用 JPype,所以我可以调用 Java 函数。我无法导入自己的 jar 文件。
我有这个罐子:/home/di/eclipse_plugins/plugins/org.eclipse.birt.report.engine_4.2.1.v20120820.jar
在org.eclipse.birt.report.engine.api 包中有一个EngineConfig 类定义。我正在尝试实例化并使用我在那个 jar 中的这个类。在常规 Java 中,这就是我所拥有的:
import org.eclipse.birt.report.engine.api.EngineConfig;
EngineConfig config = new EngineConfig();
config.setLogConfig("/home/di/logs");
我在 Python 中有这个:
import jpype
from jpype import *
jvmPath = jpype.getDefaultJVMPath()
jpype.startJVM(jvmPath, "-Djava.class.path=/home/di/eclipse_plugins/plugins/*.jar")
engineConfig = JPackage("org").eclipse.birt.report.engine.api.EngineConfig
engineConfig.setLogConfig("/home/di/logs")
jpype.shutdownJVM()
但是,当我运行它时,我得到了这个错误:
Traceback (most recent call last):
File "test.py", line 15, in <module>
engineConfig.setLogConfig()
File "/usr/lib64/python2.6/site-packages/jpype/_jpackage.py", line 53, in __call__
raise TypeError, "Package "+self.__name+" is not Callable"
TypeError: Package org.eclipse.birt.report.engine.api.EngineConfig.setLogConfig is not Callable
【问题讨论】:
-
您是否得到了上述解决方案,或者您能否分享一个从 Python 调用 java 方法的工作示例,我也面临类似的问题。
-
因此,要记住的最重要的事情之一是您的班级需要的所有班级必须在
jpype的启动 JVM 时的类路径(或稍后添加到 JVM 的类路径)。否则,jpype 在尝试查找类时会“默默地”在内部失败:当 JVM 尝试“查找”类时,它首先确保类中的所有imports 都已得到满足——如果没有, JVM 将无法找到该类。