MiraiForum

    • Register
    • Login
    • Search
    • Popular
    • Recent
    • Unsolved
    • Tags
    • Groups
    • 友情链接

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

    开发交流
    3
    8
    81
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • F
      frankcwl last edited by

      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 Reply Last reply Reply Quote 0
      • cssxsh
        cssxsh last edited by

        汇报问题请附上 mirai 版本

        F 1 Reply Last reply Reply Quote 0
        • Karlatemp
          Karlatemp last edited by 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 Replies Last reply Reply Quote 0
          • F
            frankcwl @cssxsh last edited by

            @cssxsh 版本是2.13.0-RC2

            1 Reply Last reply Reply Quote 0
            • F
              frankcwl @Karlatemp last edited by

              @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 Reply Last reply Reply Quote 0
              • F
                frankcwl @Karlatemp last edited by 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 Reply Last reply Reply Quote 0
                • Karlatemp
                  Karlatemp last edited by

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

                  F 1 Reply Last reply Reply Quote 0
                  • F
                    frankcwl @Karlatemp last edited by

                    @Karlatemp 谢谢,问题解决了

                    1 Reply Last reply Reply Quote 0
                    • 1 / 1
                    • First post
                      Last post
                    Powered by Mamoe Technologies & NodeBB | 友情链接 | 服务监控 | Contact