当前位置: 首页 > 知识库问答 >
问题:

无法使用javaagent for spring boot uber jar应用程序安装apache httpclient

林承悦
2023-03-14
onError:org.apache.http.impl.client.AbstractHttpClient
java.lang.NoClassDefFoundError: org/apache/http/HttpHost
    at java.lang.Class.getDeclaredMethods0(Native Method)
    at java.lang.Class.privateGetDeclaredMethods(Class.java:2701)
    at java.lang.Class.getDeclaredMethods(Class.java:1975)
    at net.bytebuddy.description.method.MethodList$ForLoadedType.<init>(MethodList.java:106)
    at net.bytebuddy.description.type.TypeDescription$ForLoadedType.getDeclaredMethods(TypeDescription.java:985)
    at net.bytebuddy.implementation.MethodDelegation$MethodContainer$ForExplicitMethods.ofStatic(MethodDelegation.java:1037)
    at net.bytebuddy.implementation.MethodDelegation.to(MethodDelegation.java:247)
    at net.bytebuddy.implementation.MethodDelegation.to(MethodDelegation.java:226)
    at com.yiji.dtrace.agent.httpclient4.interceptor.HttpClient4Interceptors$1.transform(HttpClient4Interceptors.java:48)
    at net.bytebuddy.agent.builder.AgentBuilder$Transformer$Compound.transform(AgentBuilder.java:457)
    at net.bytebuddy.agent.builder.AgentBuilder$Default$Transformation$Simple$Resolution.apply(AgentBuilder.java:2791)
    at net.bytebuddy.agent.builder.AgentBuilder$Default$ExecutingTransformer.transform(AgentBuilder.java:3081)
    at sun.instrument.TransformerManager.transform(TransformerManager.java:188)
    at sun.instrument.InstrumentationImpl.transform(InstrumentationImpl.java:428)
    at java.lang.ClassLoader.defineClass1(Native Method)
    at java.lang.ClassLoader.defineClass(ClassLoader.java:760)
    at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:142)
    at java.net.URLClassLoader.defineClass(URLClassLoader.java:467)
    at java.net.URLClassLoader.access$100(URLClassLoader.java:73)
    at java.net.URLClassLoader$1.run(URLClassLoader.java:368)
    at java.net.URLClassLoader$1.run(URLClassLoader.java:362)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(URLClassLoader.java:361)
    at org.springframework.boot.loader.LaunchedURLClassLoader.doLoadClass(LaunchedURLClassLoader.java:170)
    at org.springframework.boot.loader.LaunchedURLClassLoader.loadClass(LaunchedURLClassLoader.java:142)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
    at java.lang.ClassLoader.defineClass1(Native Method)
    at java.lang.ClassLoader.defineClass(ClassLoader.java:760)
    at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:142)
    at java.net.URLClassLoader.defineClass(URLClassLoader.java:467)
    at java.net.URLClassLoader.access$100(URLClassLoader.java:73)
    at java.net.URLClassLoader$1.run(URLClassLoader.java:368)
    at java.net.URLClassLoader$1.run(URLClassLoader.java:362)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(URLClassLoader.java:361)
    at org.springframework.boot.loader.LaunchedURLClassLoader.doLoadClass(LaunchedURLClassLoader.java:170)
    at org.springframework.boot.loader.LaunchedURLClassLoader.loadClass(LaunchedURLClassLoader.java:142)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
    at java.lang.ClassLoader.defineClass1(Native Method)
    at java.lang.ClassLoader.defineClass(ClassLoader.java:760)
    at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:142)
    at java.net.URLClassLoader.defineClass(URLClassLoader.java:467)
    at java.net.URLClassLoader.access$100(URLClassLoader.java:73)
    at java.net.URLClassLoader$1.run(URLClassLoader.java:368)
    at java.net.URLClassLoader$1.run(URLClassLoader.java:362)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(URLClassLoader.java:361)
at org.springframework.boot.loader.LaunchedURLClassLoader.doLoadClass(LaunchedURLClassLoader.java:170)
    at org.springframework.boot.loader.LaunchedURLClassLoader.loadClass(LaunchedURLClassLoader.java:142)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
    at com.yjf.common.net.HttpUtil.<init>(HttpUtil.java:118)
    at com.yjf.common.net.HttpUtil.<init>(HttpUtil.java:81)
    at com.yjf.common.net.HttpUtil.<clinit>(HttpUtil.java:78)
    at com.daidai.dtrace.agent.test.Main.main(Main.java:35)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:497)
    at org.springframework.boot.loader.MainMethodRunner.run(MainMethodRunner.java:53)
    at java.lang.Thread.run(Thread.java:745)
Caused by: java.lang.ClassNotFoundException: org.apache.http.HttpHost
    at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:331)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
... 60 more
public class DTraceAgent {

    public static TypeDescription abstractHttpClientDescription() {
        return new TypeDescription.Latent("org.apache.http.impl.client.AbstractHttpClient",
                Modifier.PUBLIC|Modifier.ABSTRACT,
                TypeDescription.OBJECT,
                Arrays.asList(httpClientDescription()));
    }

    public static TypeDescription httpHostDescription() {
        return new TypeDescription.Latent("org.apache.http.HttpHost",
                Modifier.PUBLIC|Modifier.FINAL,
                TypeDescription.OBJECT,
                Arrays.asList(new TypeDescription.ForLoadedType(Cloneable.class),
                        new TypeDescription.ForLoadedType(Serializable.class)));
    }

    public static TypeDescription httpContextDescription() {
        return new TypeDescription.Latent("org.apache.http.protocol.HttpContext",
                getInterfaceModifiers(),
                TypeDescription.OBJECT,
                null);
    }

    public static TypeDescription httpRequestDescription() {
        return new TypeDescription.Latent("org.apache.http.HttpRequest",
                getInterfaceModifiers(),
                httpMessageDescription(),
                null);
    }

    public static void premain(String arguments, Instrumentation instrumentation) {
        new AgentBuilder.Default()
                //.withBinaryLocator(binaryLocatorFor(instrumentation))
                .withListener(DebugListener.getListener())
                .type(is(abstractHttpClientDescription()))
                .transform(new AgentBuilder.Transformer() {
                    public DynamicType.Builder transform(DynamicType.Builder builder,
                                                         TypeDescription typeDescription) {
                        return builder.method(named("execute")
                                .and(takesArguments(httpHostDescription(), httpRequestDescription(), httpContextDescription()))
                                .and(returns(named("org.apache.http.HttpResponse"))))
                                .intercept(MethodDelegation.to(HttpClientInterceptor4dot3Plus.class));
                    }
                }).installOn(instrumentation);
    }
}

public class HttpClientInterceptor4dot3Plus {

    public static CloseableHttpResponse doExecute(
            @SuperCall Callable<CloseableHttpResponse> client, @Argument(1)HttpRequest request
            ) throws Exception {
        StringBuilder builder = new StringBuilder(1024);
        if (request != null && request.getRequestLine() != null) {
            RequestLine requestLine = request.getRequestLine();
            builder.append(requestLine.getMethod()).append(" ").append(requestLine.getUri());
        }
        try (TraceScope scope = Trace.startSpanForEntry(builder.toString())) {
            Trace.spanType(Span.SPAN_TYPE_HTTP);
            try {
                return client.call();
            } catch (Exception e) {
                Trace.exception(e);
                throw e;
            }
        }
    }
}

public class DebugListener {
    public static AgentBuilder.Listener getListener() {
        return new AgentBuilder.Listener() {
            @Override
            public void onTransformation(TypeDescription typeDescription, DynamicType dynamicType) {
                System.err.println("onTransformation:" + typeDescription.getCanonicalName());
                try {
                    dynamicType.saveIn(new File("generated_classes"));
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onIgnored(TypeDescription typeDescription) {
                //System.err.println("onIgored:" + typeDescription.getCanonicalName());
            }

            @Override
            public void onError(String typeName, Throwable throwable) {
                System.err.println("onError:" + typeName);
                throwable.printStackTrace();
            }

            @Override
            public void onComplete(String typeName) {
                //System.err.println("onComplete:" + typeName);
            }
        };
    }
}

共有1个答案

翟功
2023-03-14

我通过两个步骤解决了这个问题:

>

  • 使用Spring Boot专用的classloader来分离到卸载类型:

    public static void premain(String arguments, Instrumentation instrumentation) {
      ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
      ClassFileLocator.Compound compound = new ClassFileLocator.Compound(ClassFileLocator.ForClassLoader.of(classLoader), ClassFileLocator.ForClassLoader.ofClassPath());
      TypeDescription delegator = TypePool.Default.of(compound).describe(delegatorClass).resolve();
      new AgentBuilder.Default()
        //.withBinaryLocator(binaryLocatorFor(instrumentation))
        .withListener(DebugListener.getListener())
        .type(is(abstractHttpClientDescription()))
        .transform(new AgentBuilder.Transformer() {
          @Override
          public DynamicType.Builder transform(DynamicType.Builder builder,
              TypeDescription typeDescription) {
            return builder.method(named("execute")
              .and(takesArguments(httpHostDescription(), httpRequestDescription(), httpContextDescription()))
              .and(returns(named("org.apache.http.HttpResponse"))))
              .intercept(MethodDelegation.to(delegator));
          }
      }).installOn(instrumentation);
    } 
    

    将javaagent jar打包到Spring Boot的uber-jar中,这样委托类就可以引用与intecepted类相关的类。

  •  类似资料:
    • 我无法通过Android Studio或手动复制APK在我的联想Phab PLUS(联想PB-1 770M,Android 5.1.1 API 22)上安装我的应用程序。 通过Android Studio:我得到错误失败安装APK[INSTALL_FAILED_DEXOPT]。它提示我,手机上可能有一个现有的应用程序,带有相同的过时的德克斯罐包,并提示我卸载现有的应用程序。然而,当我继续"是",它

    • 我有一个android的设备,我在上面调试应用程序。在我生成签名apk后,我卸载了现有的调试应用程序,尝试安装签名apk,但我得到了应用程序未安装的错误。我安装在一个没有调试应用程序安装的新设备上,看起来还可以。我尝试清除应用程序数据并卸载,但没有工作。 图像错误

    • 这是我得到的错误 我甚至创建了一个新的hello world程序,但它仍然不能工作。哈普,我现在只是个实习生,这个bug阻碍了我的学习

    • 我有一个已经在商店中的应用程序,现在我正忙着做一些更改,我想在提交更新到商店之前在设备上测试。 更改后,我为iOS发送了一个调试版本。当我尝试在我的设备上安装应用程序时,什么也没发生,甚至没有显示codename-one.appspot.com想安装“应用程序名称”的常规对话框。 我还尝试在我的苹果电脑上通过iTunes安装应用程序,但安装失败。 我在使用iFunbox时也遇到了一个错误。 我的应

    • 运行安装脚本后,服务器启动并提供一个页面,在“未能编译行”之后显示相同的文本。 我正在运行Windows10和自动更新刚刚更新了我的电脑今天,就在我做创建-反应-快速安装之前。

    • 我在Play Store中有一个应用程序。 现在,我需要用更新的版本来更新它。在将其推送到Playstore之前,我想我会用APK检查迁移。 我从Playstore安装了现有版本。现在,我试图安装最新的(签名)apk。但它说,应用程序无法安装。 这个确切的案例正在与Playstore合作。我尝试了发行版。它工作正常。 我错过什么了吗。。? 提前谢谢

    • 问题内容: 我不是在寻找java-web- start,而是在寻找胖客户端应用程序安装工具包。我有一个独立的应用程序,其中包含几个文件(jar文件,数据文件等),并且需要执行一些非常标准的安装任务,例如向用户询问目标目录,让他们找到系统的某些部分- 选择一些按计算机或按用户配置的选项,并可能尝试检测它们的某些计算机设置。 我正在寻找类似于MSI或其他向导驱动的安装应用程序的东西。什么是Java的良

    • 我为我的应用程序做了一些修复,今天想在我的iPhone上试用它,现在我不能再在设备上安装它了。我在手机上收到错误“无法下载应用程序”。 我没有更改我的Apple开发者帐户上的任何内容,昨天一切都运行良好。这是一个已知的错误吗?有人知道如何修复这个问题吗?我的iPhone当然已经注册,我还做了“iOS证书向导”并重新构建了它,但它也不起作用。 编辑:所以我能够在再次执行完整的证书向导并覆盖所有内容后