最近工作需要在研究Java探针,以这篇博客为基础:https://www.cnblogs.com/sea520/p/10251838.html。
在第二篇应用实例部分,打包运行程序报错:
javassist.NotFoundException: alibaba.TimeTest
at javassist.ClassPool.get(ClassPool.java:450)
at agent.MyTransformer.transform(MyTransformer.java:57)
at java.instrument/java.lang.instrument.ClassFileTransformer.transform(ClassFileTransformer.java:246)
at java.instrument/sun.instrument.TransformerManager.transform(TransformerManager.java:188)
at java.instrument/sun.instrument.InstrumentationImpl.transform(InstrumentationImpl.java:563)
at java.base/java.lang.ClassLoader.defineClass1(Native Method)
at java.base/java.lang.ClassLoader.defineClass(ClassLoader.java:1016)
at java.base/java.security.SecureClassLoader.defineClass(SecureClassLoader.java:151)
at java.base/jdk.internal.loader.BuiltinClassLoader.defineClass(BuiltinClassLoader.java:821)
at java.base/jdk.internal.loader.BuiltinClassLoader.findClassOnClassPathOrNull(BuiltinClassLoader.java:719)
at java.base/jdk.internal.loader.BuiltinClassLoader.loadClassOrNull(BuiltinClassLoader.java:642)
at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(BuiltinClassLoader.java:600)
at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(ClassLoaders.java:178)
at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:521)
at java.base/java.lang.Class.forName0(Native Method)
at java.base/java.lang.Class.forName(Class.java:416)
at java.base/sun.launcher.LauncherHelper.loadMainClass(LauncherHelper.java:760)
at java.base/sun.launcher.LauncherHelper.checkAndLoadMain(LauncherHelper.java:655)
初步看是没找到对应的类,调试了一会,再来看报错信息,这次注意到信息第三行:
at agent.MyTransformer.transform(MyTransformer.java:57)
根据信息定位到MyTransformer.java的第57行:
ctclass = ClassPool.getDefault().get(className);
由此可知问题出在该语句上,再根据这篇博客(https://blog.csdn.net/paincupid/article/details/51175244)的提示,修改语句如下:
ClassPool pool = ClassPool.getDefault();
ClassClassPath classPath = new ClassClassPath(this.getClass());
pool.insertClassPath(classPath);
ctclass = pool.get(className);
将额外的类路径注册到ClassPool中。
再次打包运行,问题解决:
====premain方法1执行====
Hello1
=====TimeTest执行=====
hello world!!
this method sayHello cost:2001ms.
hello world222222222
this method sayHello2 cost:1001ms.
版权声明:本文为includei原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。