I do not know what javaagent is to run a hello world will know

Java development of students engaged in more or less heard java探针/ javaagentthis term. This article does not say that the definition does not say it's the principle, it is not the role of the tall, only that it's "hello world". That run one of the most simple example, take a look at the actual results, real feelings, can truly clear the entrance.

coding

Create a maven project with the idea, as shown below, the project name at random, and here I project called: microservice-comb-javaagent

20200310194628.png

Code is as follows First, create a class: AgentDemo

public class AgentDemo {
    /**
     * 该方法在main方法之前运行,与main方法运行在同一个JVM中
     */
    public static void premain(String agentArgs, Instrumentation inst) {
        System.out.println("------ premain方法 有两个入参 ------ agentArgs:" + agentArgs + " inst:" + inst.toString());
    }

    /**
     * 如果不存在 {@link AgentDemo#premain(String, Instrumentation)}, 则会执行本方法
     */
    public static void premain(String agentArgs) {
        System.out.println("------ premain方法,有一个入参 ------ agentArgs:" + agentArgs);
    }
}
复制代码

Note: AgentDemo fully qualified name tag will be in pom.xml Premain-Classcited

Secondly, and then create a class: AgentTest

public class AgentTest {
    public static void main(String[] args) {
        System.out.println(" ------ main方法");
    }
}
复制代码

Again, add the project's pom.xml

   ··· 略

<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <configuration>
                <source>1.8</source>
                <target>1.8</target>
            </configuration>
        </plugin>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-jar-plugin</artifactId>
            <configuration>
                <archive>
                    <manifestEntries>
                        <!-- 值为包含premain方法的类. 启动方式为命令行启动时,javaagent JAR文件清单必须包含 Premain-Class 属性, 代理类必须实现 public static premain()-->
                        <Premain-Class>com.skyler.cobweb.agent.AgentDemo</Premain-Class>
                        <Can-Redefine-Classes>true</Can-Redefine-Classes>
                        <Can-Retransform-Classes>true</Can-Retransform-Classes>
                    </manifestEntries>
                </archive>
            </configuration>
        </plugin>
    </plugins>
</build>
复制代码

The jar packaging

The microservice-comb-javaagentlabeled jarpackages. We will use this runtime Jarpackage packaging commands: mvn clean package. The results shown below

20200310200510.png

run

You can have two operating modes: idea中直接运行and命令行运行

  • idea directly run

Need to be configured to run under the project configuration VM options, as shown, in the form of -javaagent:jarpath[=options]; my content:-javaagent:/Users/xx/microservice-comb/microservice-comb-javaagent/target/microservice-comb-javaagent-1.0.0-SNAPSHOT.ar="hello world"

20200310211044.png

Then, run AgentTest.main method, the results as shown below

20200310201519.png

We can see, print out a method of information AgentDemo.premain

  • Command line to run cdunder the item of microservice-comb-javaagent / target / classes path
$ cd xx/target/classes
$ java -javaagent:/Users/xx/microservice-comb/microservice-comb-javaagent/target/microservice-comb-javaagent-1.0.0-SNAPSHOT.jar="hello world"  com.skyler.cobweb.agent.AgentTest
复制代码

See also print out a method of information AgentDemo.premain

20200310203649.png

reward

Here is a comparison, if coupled with the runtime -javaagent:jar, this gives you the feeling will be very clear and thorough. To again operate under

When coupled with

$ cd /Users/xx/microservice-comb/microservice-comb-javaagent/target/classes
$ java -javaagent:/Users/xx/microservice-comb/microservice-comb-javaagent/target/microservice-comb-javaagent-1.0.0-SNAPSHOT.jar="hello world" com.skyler.cobweb.agent.AgentTest
复制代码

effect

------ premain方法 有两个入参 ------ agentArgs:hello world inst:sun.instrument.InstrumentationImpl@a09ee92
 ------ main方法
复制代码

When not add

$ cd /Users/xx/microservice-comb/microservice-comb-javaagent/target/classes
$ java com.skyler.cobweb.agent.AgentTest
复制代码

effect

 ------ main方法

复制代码

The effect is obvious, without -javaagent, jar in the class method is not performed, that is not output information

The purpose of this comparison is to highlight javaagent role. And let your feelings clear role javaagent: like agent can do the same thing before the main method, the official said bytecode enhancement. By modifying the byte code, which allows developers the opportunity to do something, such as arthas and monitor components built skywalking

Not worth a thousand words runa 'hello world'

This article want the easiest, most intuitive way to keep your fastest, most real understanding of the role of javaagent. Part II Expand say javaagent principles and more practical example of the use

Guess you like

Origin juejin.im/post/5e67933e51882549315fc4ec
Recommended