Patrick
Patrick

Reputation: 1567

Transmitting Java agents instrumentation to spawned JVM

Context

I have built a custom Junit runner for my specific needs (testing Java programs that use MPI, but that's not relevant here). The key part of my runner consists in spanwing multiple JVMs to run the tests. I do this using a ProcessBuilder.

My custom runner works great, which brings me to my issue:

The problem

I am using Jacoco with Maven to create test coverage reports. On the tests that use the normal Junit framework, this works fine. However, this is not the case when my custom Junit runner is used.

More specifically, the process running my custom runner is being tracked by Jacoco (I can see it in the "sessions" page of the reports), but the JVMs spawned with the process builder are not.

Some encouraging signs: If I manually add the argument -javaagent:C:\\Users\\Patrick\\.m2\[...]\org.jacoco.agent-0.8.5-runtime.jar=destfile=C:\\[...]jacoco.exec to my process builder, it will work. The spawned JVMs are being tracked by Jacoco and I can see that the classes I am interested in are tracked. But this is not satisfactory, I can't keep this hard-coded argument.

My question

Is there a way to obtain the -javaagent part of the command from within the running JVM?

In my case, I would look for potential java agents monitoring my custom Junit runner. If there are any, I would carry them forward to the processes I spawn with the ProcessBuilder.

I looked for any signs of agents in the Properties (System.getProperties()) but I can't find anything there.

I will be happy to provide any additional information.

Upvotes: 3

Views: 201

Answers (1)

Johannes Kuhn
Johannes Kuhn

Reputation: 15163

You can get most of the VM arguments using RuntimeMXBean.getInputArguments():

List<String> vmArgs = ManagementFactory.getRuntimeMXBean().getInputArguments();

This list contains (at least on my machine) the entire -javaagent:... arguments, among other parameters.

Upvotes: 2

Related Questions