我正在尝试使用自定义类加载器动态加载elasticsearch RestClient。该类已成功加载,因为我可以看到该类中声明的所有方法(显然是通过打印它们)。但是,我无法加载将 org.apache.http。* 类作为参数传递的方法。
例如,当我这样做时:
URLClassLoader classLoader = new URLClassLoader( new URL[] {
new URL("file:///home/user/Downloads/httpclient-4.5.8.jar"),
new URL("file:///home/user/Downloads/httpasyncclient-4.1.4.jar"),
new URL("file:///home/user/Downloads/httpcore-4.4.11.jar"),
new URL("file:///home/user/Downloads/rest-5.5.3.jar")},
this.getClass().getClassLoader() );
Class<?> restClient = Class.forName("org.elasticsearch.client.RestClient", true, classLoader);
//Object restClassInstance = restClient.newInstance();
Method m[] = restClient.getMethods();
for(int i=0; i < m.length; i++) {
String name = m[i].getName();
Parameter[] params = m[i].getParameters();
Class<?> types[] = m[i].getParameterTypes();
System.out.println("Method Name : "+name);
for(int j=0; j < types.length; j++) {
String param = types[j].getName();
System.out.println("params :: "+param);
}
}
我得到的输出是:
Method Name : builder
params :: [Lorg.apache.http.HttpHost;
当我尝试加载此方法时,如下所示:
Class<?> httpHost = Class.forName("org.apache.http.HttpHost", true, classLoader);
Method restBuilderMethod = restClient.getMethod("builder",httpHost);
我收到此异常:
java.lang.NoSuchMethodException: org.elasticsearch.client.RestClient.builder(org.apache.http.HttpHost)
at java.lang.Class.getMethod(Class.java:1786)
at com.example.demo.controller.TestController.customClassLoaderImpl(TestController.java:63)
at com.example.demo.controller.TestController.main(TestController.java:27)
从以上数据中,我可以看到,当我检查构建器方法所需的参数时,它显示: [Lorg.apache.http.HttpHost;
但在例外情况下,参数显示如下: org.apache.http.HttpHost
那么我在这里想念什么吗?如果有人愿意在这方面指导我,我将不胜感激。