JavaAgent 实现字节码注入

新建MyAgent项目

JavaAgent 实现字节码注入_第1张图片

pom文件



    4.0.0

    com.book.MyAgent
    MyAgent
    1.0

    
        
            javassist
            javassist
            3.12.1.GA
        
        
            org.ow2.asm
            asm-all
            5.1
        
    

    
        
            
                org.apache.maven.plugins
                maven-compiler-plugin
                
                    1.8
                    1.8
                
            
            
                org.apache.maven.plugins
                maven-source-plugin
                3.0.1
                
                    
                        attach-sources
                        verify
                        
                            jar-no-fork
                        
                    
                
            
            
                org.apache.maven.plugins
                maven-assembly-plugin
                2.6
                
                    
                        jar-with-dependencies
                    
                    
                        src/main/resources/META-INF/MANIFEST.MF
                    
                
                
                    
                        assemble-all
                        package
                        
                            single
                        
                    
                
            
        
        
            
                ${basedir}/src/main/resources
            
            
                ${basedir}/src/main/java
            
        
    

AgentMain.java

import org.objectweb.asm.ClassReader;
import org.objectweb.asm.ClassVisitor;
import org.objectweb.asm.ClassWriter;
import org.objectweb.asm.MethodVisitor;
import org.objectweb.asm.Opcodes;

import java.lang.instrument.ClassFileTransformer;
import java.lang.instrument.IllegalClassFormatException;
import java.lang.instrument.Instrumentation;
import java.security.ProtectionDomain;

public class AgentMain {
	public static void premain(String agentOps, Instrumentation inst) {
            System.out.println("=========premain方法执行========");
            System.out.println(agentOps);
            //instrument(agentOps, inst);
	}

	public static void agentmain(String agentOps, Instrumentation inst) {
		instrument(agentOps, inst);
	}

 src\main\resources\META-INF下新建MANIFEST.MF

Manifest-Version: 1.0
Premain-Class: com.book.myagent.AgentMain
Agent-Class: com.book.myagent.AgentMain
Can-Redefine-Classes: true

注意会生成3个jar

新建第二个项目Test:

 

package com.book.test;


public class HelloWorld {
    public static void main(String[] arg) {
    System.out.println("输出>>>HelloWorld");
    }
}

pom.xml



    4.0.0
    com.book
    Test
    1.0-SNAPSHOT
    jar
    
        UTF-8
        1.8
        1.8
    
    
        
                
                maven-assembly-plugin    
                    
                    false    
                        
                        jar-with-dependencies    
                        
                        
                            
                            com.book.test.HelloWorld    
                            
                        
                    
                    
                        
                        make-assembly    
                        package    
                            
                            assembly    
                            
                        
                    
            
        
    

 java  -javaagent:E:\worktest\MyAgent\target\MyAgent-1.0-jar-with-dependencies.jar -jar E:\worktest\Test\target\Test-1.0-SNAPSHOT.jar com.book.test.HelloWorld

 

执行:

java  -javaagent:E:\worktest\MyAgent\target\MyAgent-1.0-jar-with-dependencies.jar -jar E:\worktest\Test\target\Test-1.0-SNAPSHOT.jar com.book.test.HelloWorld

注意不在pom设置默认主类可以用 java -cp启动

JavaAgent 实现字节码注入_第2张图片

参考:https://github.com/liuzhengyang/javaagent-example

https://blog.csdn.net/catoop/article/details/51034739 

你可能感兴趣的:(Java)