Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I import user built jars in Python using JPype?

Tags:

java

python

jpype

I am using JPype in Python so I can call Java functions. I am having trouble importing my own jar files.

I have this jar: /home/di/eclipse_plugins/plugins/org.eclipse.birt.report.engine_4.2.1.v20120820.jar

In the org.eclipse.birt.report.engine.api package there is a EngineConfig class definition. I am trying to instantiate and use this class I have in that jar. In regular Java this is what I would have:

import org.eclipse.birt.report.engine.api.EngineConfig;

EngineConfig config = new EngineConfig();     
config.setLogConfig("/home/di/logs");

I have this in 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() 

However, when I run this, I get this error:

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
like image 321
Di Zou Avatar asked Nov 12 '22 07:11

Di Zou


1 Answers

I was not able to reproduce exactly the same error (instead I got a "RuntimeError: No matching overloads found"). Though, I see a problem in your Python code:

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

What you get in engineConfig is a Class.

setLogConfig() is not a static method, so you have to instantiate the EngineConfig class first:

# Get EngineConfig class
EngineConfig = JPackage("org").eclipse.birt.report.engine.api.EngineConfig
# Instantiate EngineConfig
engineConfig = EngineConfig()
# Call method
engineConfig.setLogConfig("/home/di/logs")
like image 114
Alex Baguet Avatar answered Nov 14 '22 21:11

Alex Baguet