Tutorials about javaagents
Solution 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:
-
Implement a static
premain
(as an analogy tomain
) method, like this:import java.lang.instrument.Instrumentation; class Example { public static void premain(String args, Instrumentation inst) { ... } }
-
Create a manifest file (say,
manifest.txt
) marking this class for pre-main execution. Its contents are:Premain-Class: Example
-
Compile the class and package this class into a
JAR
archive:javac Example.java jar cmf manifest.txt yourAwesomeAgent.jar *.class
-
Execute your JVM with
-javaagent
parameter, like this:java -javaagent:yourAwesomeAgent.jar -jar yourApp.jar
Solution 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
alicjasalamon
UX student at Tampere University of Technology in Finland
Updated on July 20, 2020Comments
-
alicjasalamon almost 4 years
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:- An agent programmed in Java being an incarnation of the agent concept of distributed systems.
- 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.
-
Vadzim over 11 yearsstackoverflow.com/questions/1277219/… also has some useful links
-
berezovskyi almost 10 yearsIt is important that the
-javaagent
parameter goes before the-jar
parameter. -
11101101b over 9 yearsI had to add an additional entry to my manifest to get it to work with using Javassist. You can see in my answer to this question: stackoverflow.com/questions/10423319/…
-
end-user over 8 yearsI recently followed these steps to build an agent. I kept running into problems creating the jar, until I found out that the command should have listed the target .jar file first and the manifest file after.
-
Searene over 5 yearsYou may also need to provide an empty
main
method in theExample
class, or an error may be thrown complaining about the lack of the main method. -
villanueva.ricardo over 4 yearsremember to finish the manifest.txt whit a blank line