5

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
Di Zou
  • 4,469
  • 13
  • 59
  • 88
  • 1
    Did you get solution for above, or could you please share a working example of calling java method from Python, I am also facing similar issue. – Sankalp Jan 22 '17 at 19:15
  • So, one of the really important things to remember is that all of the classes **needed** by your class *must* be on `jpype`'s classpath when you start the JVM (or otherwise added to the JVM's classpath later). Otherwise, jpype will "silently" internally fail when trying to find the class: when the JVM tries to "find" the class, it first makes sure that all of the `import`s in the class have been satisfied -- if not, the JVM will not be able to find the class. – Marco Jan 31 '19 at 13:49

1 Answers1

1

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")