Initial knowledge javaagent of Java Reverse Foundation

Source: Internet
Author: User

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.