New candidate JEP: 451: Prepare to Disallow the Dynamic Loading of Agents

Alan Bateman Alan.Bateman at oracle.com
Fri May 12 06:14:28 UTC 2023


On 11/05/2023 22:55, Bruno Borges wrote:
>
>
> In the cases I've seen, it is the application that is loading the 
> agent programmatically, first thing they do in their static_void_main 
> method.
>
> This has some benefits like allowing the application to depend on an 
> agent as they do with any other library, through a dependency 
> management tool (Maven/Gradle), and having more easily version 
> upgrading mechanisms.
>
> The -javaagent deployment model is not an option to this, nor is the 
> flag to enable the programmatically approach, as it requires the 
> application *developer* to tune JVM flags to do so.
>
> Only possible solution I thought about was to give jlink a flag that 
> would enable the feature. I wonder if this has been considered.
>

It's good to bring up the Maven/Gradle environments where testing 
(mocking in particular) can involve agents. For some reason, these 
agents are incorrectly added to the class path when testing rather than 
via the -javaagent option, that is something that the maintainers of the 
build configuration tools or plugins could look into.

An application main loading an agent into the current VM sounds dubious. 
The attach mechanism was never intended to support an application to do 
brain surgery on itself. It was a mistake (in JDK 6) that this ever 
worked but it took until JDK 9 before this was disabled by default. 
There may be cases where an application ships with some APM capabilities 
that involve an agent - that's the scenario that Launcher-Agent-Class 
was added for in Java 9. It allows an executable JAR to contain both an 
application (Main-Class) and an agent (Launcher-Agent-Class), with the 
agent started before the application runs.

jlink --add-options "-javaagent:agent.jar" will do what you are want but 
the JAR file containing the agent will reside outside of the run-time 
image. Right now there isn't any support for building/deployment as 
agent as a module, it's something that has been kicked down the road a 
few times and probably worth dusting off at some point.

-Alan


More information about the jdk-dev mailing list