Tutorials about javaagents

JavaJavaagents

Java Problem Overview


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.

Java Solutions


Solution 1 - Java

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
    

Solution 2 - Java

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

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

[1]: https://zeroturnaround.com/rebellabs/how-to-inspect-classes-in-your-jvm/ "How-to guide to writing a javaagent | zeroturnaround.com" [2]: https://speakerdeck.com/shelajev/taming-javaagents-bcn-jug-2015 [3]: http://docs.oracle.com/javase/8/docs/api/java/lang/instrument/package-summary.html

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionalicjasalamonView Question on Stackoverflow
Solution 1 - JavanpeView Answer on Stackoverflow
Solution 2 - JavadevmakeView Answer on Stackoverflow