I have two separate projects in my IDE for the agent and for the loader that finds a target VM and loads the agent JAR.
- When the agent project is built, the resulting agent JAR artifact is copied into the loader's resources folder.
- When the loader project is built, the loader JAR contains both loader code and the
agent.jarin it.
The resulting runnable loader structure looks like this:
loader.jar
├── META-INF
│ └── MANIFEST.MF
├── me.domain.loader
│ └── Main.class
└── agent.jar
├── META-INF
│ └── MANIFEST.MF
└── me.domain.agent
└── Agent.class
From the VirtualMachine#loadAgent(java.lang.String) specification, I need to provide a path to the JAR containing the agent as the first parameter.
However, when using Main.class.getResource("/agent.jar").getPath() I'm getting an AgentLoadException: Agent JAR not found or no Agent-Class attribute. What's the correct way to do that?
It looks like the agent JAR to be loaded must exist on disk. I've solved this issue by copying the embedded JAR resource into a temporary file:
I'm then using this temporary path to load the agent: