Tutorials about javaagents [closed]

独自空忆成欢 提交于 2019-11-26 05:16:36

问题


I\'d like to learn something about javaagents, but researching is not easy. Most of result refers to JADE. I know java agent can mean two things:

  1. An agent programmed in Java being an incarnation of the agent concept of distributed systems.
  2. A low-level software component to augment the working of a JVM, such as profilers, code-coverage tools, etc

I\'ve found similar question here, but unfortunately it also refers to version 1.

Do you know any articles, tutorials for beginners, sample project about javaagent in version 2? I\'ve found one here, but I\'m looking for more.


回答1:


The second case talks about Java Instrumentation API - this link points to a Javadoc which is rather descriptive.

And here, is the full instruction and an example of how to create java instrumentation agent.

The main concept is to:

  1. Implement a static premain (as an analogy to main) method, like this:

    import java.lang.instrument.Instrumentation;
    
    class Example {
        public static void premain(String args, Instrumentation inst) {
            ...
        }
    }
    
  2. Create a manifest file (say, manifest.txt) marking this class for pre-main execution. Its contents are:

    Premain-Class: Example
    
  3. Compile the class and package this class into a JAR archive:

    javac Example.java
    jar cmf manifest.txt yourAwesomeAgent.jar *.class
    
  4. Execute your JVM with -javaagent parameter, like this:

    java -javaagent:yourAwesomeAgent.jar -jar yourApp.jar
    



回答2:


Few useful resources for the javaagent as described in point #2.

  • How-to guide to writing a javaagent
  • Taming Javaagents - presentation at BCN JUG 2015
  • API documentation for java.lang.instrument


来源:https://stackoverflow.com/questions/11898566/tutorials-about-javaagents

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!