First of all, what is javaagent?
Javaagent is a way to modify bytecode without affecting the normal compilation.
In the reverse, Javaagent can complete the interception and enhancement of the class.
See an example
Create a project with the following myagent structure in eclipse
Myagent.java File Contents
package com.vvvtimes.demo.agent;import java.lang.instrument.instrumentation;public class myagent { public Static void premain (String agentops, instrumentation inst) { system.out.println ("The number of =========premain parameters is 2 method execution ========,my agentops = [" + agentOps + "].); system.out.println (Agentops); } public static void premain (String agentops) { system.out.println ("The number of =========premain parameters is 1 method execution ========,my agentops = [" + agentOps + "].); system.out.println (agentops); }}
MANIFEST. MF File contents
Manifest-version:1.0premain-class:com.vvvtimes.demo.agent.myagentcan-redefine-classes:true
Note: The premain-class here must point to the class where the Premain method resides
This file needs to be specified manually when eclipse exports the jar.
We name the exported jar Myagent.jar
and build a project called MyProgram.
The code is as follows
Package Com.vvvtimes.demo;public class MyProgram {public static void main (string[] args) {System.out.println ("========= Main method Execution ======== ");}}
Export runnable jar named Myprogram.jar
Put these two jars in the same directory (I put them on the desktop)
After the CD switches to the directory, execute
Java-javaagent:myagent.jar=helloworld-jar Myprogram.jar
The operation results are as follows
C:\users\admin\desktop>java-javaagent:myagent.jar=helloworld-jar myprogram.jar=========premain parameter number is 2 method execution = = ====,my Agentops = [Helloworld].helloworld=========main method Execution ========
You can see that the main method executes the two output statements in the Premain method before executing
Javaagent parameter Options
The location of the Java-javaagent:agentjar file [= parameters passed in Premain]-jar the jar file to run
Initial knowledge javaagent of Java Reverse Foundation