如何使用JPype在Python中导入用户构建的jar?

时间:2013-04-11 21:16:12

标签: java python jpype

我在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类定义。我试图实例化并使用我在那个罐子里的这个类。在常规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

1 个答案:

答案 0 :(得分:1)

我无法重现完全相同的错误(相反,我得到了“RuntimeError:找不到匹配的重载”)。但是,我发现你的Python代码存在问题:

engineConfig = JPackage("org").eclipse.birt.report.engine.api.EngineConfig

您在engineConfig中获得的是一个类。

setLogConfig()不是静态方法,因此您必须首先实例化EngineConfig类:

# Get EngineConfig class
EngineConfig = JPackage("org").eclipse.birt.report.engine.api.EngineConfig
# Instantiate EngineConfig
engineConfig = EngineConfig()
# Call method
engineConfig.setLogConfig("/home/di/logs")