How to debug a Java agent
Asked Answered
H

1

10

At the moment there is possibility to run JVM with an agent:

-javaagent:somejar.jar

this way in order to run the Java agent I have wrote as an I have to put it into a jar. therefore it is limited in debugging. Is there a technique for debugging a Java agent in IDE while attaching it to a JVM?

Hachmann answered 12/1, 2016 at 10:48 Comment(1)
I think it is worth mentioning that for me at least, breakpoints in premain method never trigger. But in my ClassFileTransformer for example which is an inner class of my agent, the breakpoints do trigger.Gobo
T
8

The debugging API does not distinguish between a Java agent and "normal" code. If you are for example keeping your agent's sources in the same project, you can set breakpoints as you expect it. Your IDE will then simply register the source file's names and line numbers to the debugging API as it normally does.

Terret answered 13/1, 2016 at 11:15 Comment(4)
I just can not see how to attach "normal code" to JVM as an agent while debugging on. Would you emphasize please!Hachmann
You bundle your agent as you are used to but you are importing the agents source code into your project. This way, you can inform your IDE about source file and line number where you want to break. Commonly, you make the agent a subproject.Terret
It also works having the agent code inside your project, just like ordinary code, and create/specify an agent jar consisting of a manifest only. That also allows you to update the agent code easily without having to rebuild the jar every time…Aberdeen
@Aberdeen Awesome tip!Vinculum

© 2022 - 2024 — McMap. All rights reserved.