'Getting `java.lang.ClassNotFoundException: org.apache.commons.pool.PoolableObjectFactory` when using Java reflection
I am trying to invoke a function using java reflection. My initial program is written in Ballerina which is given below.
import ballerina/http;
function __getQueryResults() returns error|string{
http:Client myClient = check new("https://disease.sh");
http:Response response = check myClient->get("/v3/covid-19/all");
return response.getTextPayload(); }
I am creating a Ballerina project with above function, build the project, and I am getting a jar file as the result.
Then I get the URLClassLoader of the jar file as below
URL pathUrl = Paths.get(executablePath).toUri().toURL();
URLClassLoader classLoader = AccessController.doPrivileged((PrivilegedAction<URLClassLoader>) () ->
new URLClassLoader(new URL[]{pathUrl}, ClassLoader.getSystemClassLoader()));
and trying to invoke the __getQueryResults
function by invoking $configureInit
, $moduleInit
, $moduleStart
and __getQueryResults
orderly.
I am getting the below exception when trying to invoke $moduleInit
function
java.lang.NoClassDefFoundError: org/apache/commons/pool/PoolableObjectFactory
Any possible causes or solutions to this issue ?
Solution 1:[1]
This NoClassdEfError happens if the internal data structure of the class definition cannot be find or if the previous attempt to load the class was failed due to class path issue.
BTW we do not recommend to use java reflection to invoke ballerina function from java native side, since async code will not work properly since we do not properly schedule ballerina function call.
We have discussed same issue in https://github.com/ballerina-platform/ballerina-lang/issues/34793 and decided to generate ballerina project with the main function which calls the required ballerina function, and use the bal run command to execute the generated project.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | WaruApz |