Java agents - old innovation

Very often the first version of given language influences programming knowledge. When somebody started with Java 6 and has never used Java agent, this feature introduced in Java 5 can appear mysterious.

Data Engineering Design Patterns

Looking for a book that defines and solves most common data engineering problems? I'm currently writing one on that topic and the first chapters are already available in πŸ‘‰ Early Release on the O'Reilly platform

I also help solve your data engineering problems πŸ‘‰ contact@waitingforcode.com πŸ“©

In this article we'll try to demystify this part. At the begin, we'll explain what Java agent is. The second part will give some real use cases of Java agents.

What is Java agent ?

As already told, agents were introduced with Java 5 and are very often referenced to the arrival of java.lang.instrument package, still within the same version. This package provides services helping to instrument programs running on the JVM.

What does "instrument" exactly mean ? It could be understood as a way to communicate with program running on the JVM. Thanks to this communication, which should be written with the use of java.lang.instrument services, we can redefine classes. This redefinition means the ability to modify class methods before their final definition in the JVM. Everything can be done thanks to appropriated members of instrument package. So to resume, agent can be thought as an interceptor triggered directly before the program execution (before program's main() method invocation).

Java agents must be packaged in JAR archives and must contain correctly formatted manifest file defining allowed actions. Code details will be provided in one of next articles. This time, we'll focus only on code modification part.

Once packaged, agent definition is appended to JVM arguments -javaagent:agent.jar. This command can be define one or more times. Each agent can also take some command line options thanks to optional "=" clause. Full command line executing an agent with options could look like -javaagent:agent.jar=option1,option2,option3.

Java agent use cases - class inspection

In the real world, Java agent can be used to different purposes. One of them can be recording of test coverage information. An example of this use is the agent of Jacoco test coverage library.

Another real world use case is Aspect-Oriented Programming (AOP). As an example we can take AspectJ framework with its feature of weaving. One of weaving types, load-time, is based on Java agents work. Thanks to it, AspectJ weaving agent can change the code of given class, regarding to its configuration, before making it ready to use in the JVM. We can say that original classes are decorated with new features generated by AspectJ.

Also some profiling tools must be executed with Java agents. One of them is the JRebel 5. From almost the same family, we can also mention the tools used to estimate memory size occupied by some objects. One of them is JAMM, Java Agent for Memory Measurements..

This article doesn't tell everything about Java agents. It only presents some basic informations, such as the general logic of java.lang.instrument package and the way of associating agents to executed program. The second part shows some projects where Java agents are used to manipulate loaded classes.


If you liked it, you should read:

πŸ“š Newsletter Get new posts, recommended reading and other exclusive information every week. SPAM free - no 3rd party ads, only the information about waitingforcode!