This project demonstrates how to create and use a Java instrumentation agent. It consists of two main components:
- Agent: The instrumentation agent responsible for modifying the behavior of the target application.
- Application: A simple vending machine application to be instrumented.
The Application module represents a straightforward implementation of a vending machine.
To compile and package the application, navigate to the Application directory and execute the following command:
mvn clean packageThis will generate the application JAR file in the target directory.
The Agent module contains the code for the Java instrumentation agent.
To compile and package the agent, run the following command from the Agent directory:
mvn clean packageThe resulting JAR file will be located in the target directory.
There are two ways to load a Java instrumentation agent: Premain (static loading) and Agentmain (dynamic loading). Below are detailed instructions for both methods.
In static loading, the agent is loaded at JVM startup using the -javaagent flag.
- Navigate to the root directory of the project.
- Run the following command:
java -javaagent:./Agent/target/Agent-jar-with-dependencies.jar -jar ./Application/target/Application-jar-with-dependencies.jarThis will start the application with the agent attached, allowing the agent to modify the application during startup.
In dynamic loading, the agent is attached to a running JVM process at runtime.
- First, start the application using the following command:
java -jar ./Application/target/Application-jar-with-dependencies.jar- Then, load the agent dynamically into the running application:
java -jar ./Agent/target/Agent-jar-with-dependencies.jarBy following these steps, you can successfully experiment with Java instrumentation using the provided sample project.