dependencies中加入org.apache.httpcomponents:httpclient后运行报错
-
java初学者,想试着开发插件玩一下,遇到了以下问题。
在build.gradle.kts中的dependencies加入了implementation("org.apache.httpcomponents:httpclient:4.5.13")
能顺利通过编译,但是程序每次执行到HttpClients.createDefault()方法时,就会有该报错
java.lang.LinkageError: loader constraint violation: when resolving method "org.apache.http.impl.client.CloseableHttpClient.execute(Lorg/apache/http/client/methods/HttpUriRequest;)Lorg/apache/http/client/methods/CloseableHttpResponse;" the class loader (instance of net/mamoe/mirai/console/internal/plugin/JvmPluginClassLoaderN) of the current class, com/frankcwl/Majsoul, and the class loader (instance of net/mamoe/mirai/console/internal/plugin/DynLibClassLoader) for the method's defining class, org/apache/http/impl/client/CloseableHttpClient, have different Class objects for the type org/apache/http/client/methods/HttpUriRequest used in the signature
可能是因为引入的包和mirai依赖的冲突了,但是如果不在dependencies中添加的话,编译时就会报错Cannot resolve symbol 'HttpClients'
本人之前没有接触过gradle,请问该怎么修改 -
汇报问题请附上 mirai 版本
-
执行以下代码片段
fun Class<*>.dump() { println(this) println("|- CCL ${this.classLoader}" } override fun onEnable() { Class.forName("org.apache.http.client.methods.HttpUriRequest").dump() MyPluginMain::class.java.classLoader.loadClass("org.apache.http.client.methods.HttpUriRequest").dump() }
-
@cssxsh 版本是2.13.0-RC2
-
2022-11-17 10:22:04 I/stdout: interface org.apache.http.client.methods.HttpUriRequest 2022-11-17 10:22:04 I/stdout: |- CCL DynLibClassLoader{IndependentCL{majsoul-dev.mirai2.jar}} 2022-11-17 10:22:04 I/stdout: interface org.apache.http.client.methods.HttpUriRequest 2022-11-17 10:22:04 I/stdout: |- CCL DynLibClassLoader{IndependentCL{majsoul-dev.mirai2.jar}}
-
@Karlatemp 因为插件使用java写的,我觉得在kotlin环境下运行可能得到的结果不一样,所以我尝试在java中加入了以下代码
public void onEnable() { try { dump(Class.forName("org.apache.http.client.methods.HttpUriRequest")); dump(Majsoul.class.getClassLoader().loadClass("org.apache.http.client.methods.HttpUriRequest")); } catch (ClassNotFoundException e) { throw new RuntimeException(e); } } void dump(Class<?> c) { System.out.println(c); System.out.println("|- CCL " + c.getClassLoader()); }
得到的结果是
2022-11-17 10:48:42 I/stdout: interface org.apache.http.client.methods.HttpUriRequest 2022-11-17 10:48:42 I/stdout: |- CCL sun.misc.Launcher$AppClassLoader@55f96302 2022-11-17 10:48:42 I/stdout: interface org.apache.http.client.methods.HttpUriRequest 2022-11-17 10:48:42 I/stdout: |- CCL DynLibClassLoader{IndependentCL{majsoul-dev.mirai2.jar}}
-
请不要提前使用引入的依赖,请在onEnable后才使用
-
@Karlatemp 谢谢,问题解决了