javaagent+javassist encountered problems in implementing fault injection

 

ClassPool pool = ClassPool.getDefault(); 

CtClass ctclass = pool.get(className);

 

The above two lines of code cannot get the Class object under the web container. The official explanation is as follows:

The default ClassPool returned by a static method ClassPool.getDefault() searches the same path that the underlying JVM (Java virtual machine) has. If a program is running on a web application server such as JBoss and Tomcat, the ClassPool object may not be able to find user classes since such a web application server uses multiple class loaders as well as the system class loader. In that case, an additional class path must be registered to the ClassPool. Suppose that pool refers to a ClassPool object:  

 

Modify it to the following code:

                                ClassPool pool = ClassPool.getDefault(); 

                                pool.insertClassPath(new ClassClassPath(this.getClass()));

 

                                CtClass ctclass = pool.get(className);

 

Then, put the agent file developed by yourself, such as: myagnet.jar, and put the jar into the target system location /WEB-INF/lib/ and publish it to the web container together.

MANIFEST.MF configuration file for myagent.jar:

Manifest-Version: 1.0

Premain-Class: XXXXXXXXXXXXXXX.client.XXXXAgent

Can-Redefine-Classes: true

Class-Path: dubbo-2.5.3.jar javassist-3.19.0-GA.jar

// The last line of this file must be empty

 

myagent.jar depends on the two dubbo-2.5.3.jar javassist-3.19.0-GA.jar, these two files must exist under /WEB-INF/lib

 

If the injected target system is not a web environment, the three file jars (myagent.jar, dubbo-2.5.3.jar, javassist-3.19.0-GA.jar) can be in the same location.

 

To configure VM startup parameters on the target system, add the following line:

-javaagent:D:\XXX\tomcat-7.0.52\webapps\project name\WEB-INF\lib\myagent.jar

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326135798&siteId=291194637