Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Tutorials about javaagents [closed]

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.

like image 944
alicjasalamon Avatar asked Aug 10 '12 09:08

alicjasalamon


People also ask

What is the purpose of Javaagent?

In general, a java agent is just a specially crafted jar file. It utilizes the Instrumentation API that the JVM provides to alter existing byte-code that is loaded in a JVM. For an agent to work, we need to define two methods: premain – will statically load the agent using -javaagent parameter at JVM startup.

How do I create a Javaagent?

To create a successful javaagent we'll need four things: an agent class, some meta-information to tell the JVM what capabilities to give to our agent class, a way to make the JVM load the . jar with the agent before it starts minding the application's business and a coffee.

What is Javaagent option?

Java agents are a special type of class which, by using the Java Instrumentation API, can intercept applications running on the JVM, modifying their bytecode.


1 Answers

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 
like image 80
npe Avatar answered Oct 10 '22 18:10

npe