MiraiForum

    • 注册
    • 登录
    • 搜索
    • 热门
    • 最新
    • 未解决
    • 标签
    • 群组
    • 友情链接

    dependencies中加入org.apache.httpcomponents:httpclient后运行报错

    开发交流
    3
    8
    527
    正在加载更多帖子
    • 从旧到新
    • 从新到旧
    • 最多赞同
    回复
    • 在新帖中回复
    登录后回复
    此主题已被删除。只有拥有主题管理权限的用户可以查看。
    • F
      frankcwl 最后由 编辑

      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,请问该怎么修改

      1 条回复 最后回复 回复 引用 0
      • cssxsh
        cssxsh 最后由 编辑

        汇报问题请附上 mirai 版本

        F 1 条回复 最后回复 回复 引用 0
        • Karlatemp
          Karlatemp 最后由 Karlatemp 编辑

          执行以下代码片段

          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()
          }
          
          F 2 条回复 最后回复 回复 引用 0
          • F
            frankcwl @cssxsh 最后由 编辑

            @cssxsh 版本是2.13.0-RC2

            1 条回复 最后回复 回复 引用 0
            • F
              frankcwl @Karlatemp 最后由 编辑

              @Karlatemp

              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}}
              
              1 条回复 最后回复 回复 引用 0
              • F
                frankcwl @Karlatemp 最后由 frankcwl 编辑

                @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}}
                
                1 条回复 最后回复 回复 引用 0
                • Karlatemp
                  Karlatemp 最后由 编辑

                  请不要提前使用引入的依赖,请在onEnable后才使用

                  F 1 条回复 最后回复 回复 引用 0
                  • F
                    frankcwl @Karlatemp 最后由 编辑

                    @Karlatemp 谢谢,问题解决了

                    1 条回复 最后回复 回复 引用 0
                    • 1 / 1
                    • First post
                      Last post
                    Powered by Mamoe Technologies & NodeBB | 友情链接 | 服务监控 | Contact