在web程序中,我希望能够收集运行在tomcat中所有程序的异常抛出捕获来完成一个简单的AOP切入功能,查阅相关资料可以用JVMTI写一个agentlib,放在java命令的启动参数列表中。
我用C++写了一个libExceptCatch.so,实现里面的EVENT_EXCEPTION回调,如下图
然后java端写了一个ExceptionHandler,并打包为JVMTITest.jar
在Tomcat的webapps/my下写的index.jsp如下
启动tomcat的时候在CATALINA_OPTS中加入-agentpath,在CLASSPATH中加入JVMTITest.jar,如下截图
启动tomcat访问jsp,tomcat直接奔溃了,报NoClassDefFoundError。
我查阅JNI Referrence说 "Since JDK 1.2, when FindClass is called through the Invocation Interface, there is no current native method or its associated class loader. In that case, the result of ClassLoader.getSystemClassLoader is used. This is the class loader the virtual machine creates for applications, and is able to locate classes listed in the java.class.path property.“,这是ClassLoader的问题么
我又另外写了一个独立的类,不放在tomcat下,这个就能正常运行。
但是如果我把类名过滤给去掉,也会报NoClassDefFoundError,因为在启动虚拟机的时候加载某些jar包的时候找不到文件出错也会调上面的回调函数。下图是把类名过滤去掉同时把FindClass也去掉产生的Exception输出,指示jdk/ext下的某些jar不存在
总体感觉是不同时机长生的不同异常事件用的ClassLoader不一样,我就不知道我怎么能在适当的时候能拿到正确的ClassLoader,因为我也没有做其他额外的事情。
我也是才学,很多东西不太懂,有没有专业人士能够指点一二.
万分感谢!