How to Attach to a Running Java Process
When running Java programs, it is advantageous to monitor those programs and their execution externally. You may want to attach a monitoring tool to the program's process to record the state of the program. By using the "attach" API provided in the Java Development Kit (JDK), you can attach a monitoring agent to a running Virtual Machine instance, which represents a running program, and monitor the activity.
Instructions
-
-
1
Create a Virtual Machine object to attach to the process through its unique ID:
import java.lang.management.\*;
import java.io.\*;
import java.util.\*;
import javax.management.\*;
import javax.management.remote.\*;
import com.sun.tools.attach.\*;public class testing{
public static void main(String[] args){
VirtualMachine vm = VirtualMachine.attach(processid);
}
} -
2
Select an agent program (usually a jar):
VirtualMachine vm = VirtualMachine.attach(processid);
String agent = "C:\...agent_name.jar"; // variable depending on agent program -
-
3
Attach the agent to the Virtual Machine process to monitor:
VirtualMachine vm = VirtualMachine.attach(processid);
String agent = "C:\...agent_name.jar";
vm.loadAgent(agent);
-
1