Reputation: 1859
I have just upgraded from JDK 17 to 21 in my Windows machine. After that when I am running JUNIT test and I am getting the following warring. I have looked over all of the day, but no solution worked for me. Any feedback please:
WARNING: A Java agent has been loaded dynamically (...\byte-buddy-agent-1.14.9.jar)
WARNING: If a serviceability tool is in use, please run with -XX:+EnableDynamicAgentLoading to hide this warning
WARNING: If a serviceability tool is not in use, please run with -Djdk.instrument.traceUsage for more information
WARNING: Dynamic loading of agents will be disallowed by default in a future release
Upvotes: 45
Views: 24746
Reputation: 119
In case if you are using Gradle, you can add these options to your build configuration to get rid of the warnings.
build.gradle:
test {
jvmArgs("-XX:+EnableDynamicAgentLoading")
}
Upvotes: 10
Reputation: 6786
For me the same was happening with Mockito agent. Registering it with surefire plugin resolved it in command line and IDE:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<argLine>-javaagent:${settings.localRepository}/org/mockito/mockito-core/${mockito.version}/mockito-core-${mockito.version}.jar</argLine>
</configuration>
</plugin>
mockito.version
should be defined as property (or provided by parent pom, such as spring-boot).
Note that Mockito suggests to use maven-dependency-plugin, but that way may not be resolved properly by IDEs.
Upvotes: 9
Reputation: 1002
If you will run the test from Eclipse, you need to change the JUnit Run Configuration.
I added the following to Eclipse JUnit Run Configuration VM Arguments:
-Xshare:off
-javaagent:${env_var:userprofile}\.m2\repository\org\mockito\mockito-core\5.14.2\mockito-core-5.14.2.jar
Note: Change the path to match the location of the jar file on your local. There is no variable to point to the designated Mockito JAR so the above was the best I could do.
And all warnings were cleared with the test execution.
Below are the configurations I used in pom.xml:
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<executions>
<execution>
<goals>
<goal>properties</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>3.5.2</version>
<configuration>
<argLine>
-javaagent:${org.mockito:mockito-core:jar}
-Xshare:off
</argLine>
</configuration>
</plugin>
</plugins>
</build>
Note that I removed @{argLine}
as it was causing the Maven Test goal to break.
I should have known that Maven and Eclipse are different animals that are not 100% integrated.
Upvotes: 1
Reputation: 1600
If you have IntelliJ, your project uses Gradle and you're running tests using run configurations of type JUnit
instead of Gradle
, the way to add the -XX:+EnableDynamicAgentLoading
VM option by default to all run configurations is to edit the template for the JUnit ones.
Go to Run
-> Edit Configurations...
, then click on the JUnit
root element, and at the bottom click on Edit configuration templates...
Then add the VM option in the text box that already contains "-ea", and save.
As you can see by the message at the top in the 2nd image, you'll also have to add the option in each pre-existing run configuration.
Upvotes: 2
Reputation: 1720
As few people have already suggested, this happens due to JEP 451. Allowing the dynamic loading of agents using the -XX:+EnableDynamicAgentLoading
option is not a good solution. It just masks the problem. And even if you allow it for some reason, you should really avoid loading agents dynamically anyway. Upcoming versions of Java will get you in trouble this way.
Instead, following the Mockito's guide (in case you are using Mockito, of course), you can load the agent properly (and yes, you can directly load mockito-core
as an agent, no need to load byte-buddy-agent
in this case; the error message might be a little misleading). If you are not using Mockito, the process is the same, just point at the agent that needs to be loaded according to the error message (e.g. net.bytebuddy:byte-buddy-agent:jar
).
From your question I see your problem is with IntelliJ. The above solution fixes the problem only when you use a build tool to run your tests (e.g. when you run mvn test
on the command line), but if you want to just click on the green arrow on the left side of your test and run it from there, the message will still be present. The reason is that this way you execute directly the java
command and the build tool is not involved at all. A way to mitigate this is to update your run configuration by appending to the VM options filed the -javaagent
property (usually, you will already have -ea
set there).
The catch here is that since you are not using a build tool, you can't get automatically the path to the agent's JAR, like you would have it otherwise (e.g. ${org.mockito:mockito-core:jar}
in case of Maven + Mockito). Instead, you can do this:
-javaagent:$MAVEN_REPOSITORY$/org/mockito/mockito-core/5.14.2/mockito-core-5.14.2.jar
Unfortunately, as you can see, you need to point to a specific Mockito version since you can't know at this point the version used by your build tool.
Another catch is that you need to use this specific run configuration to run your test. If you click instead on the green arrow on the left side of the test class/method, IntelliJ will create a new run configuration that doesn't have your VM option set. To get around this you can edit your run configuration template for JUnit, but this would mean that you will have this specific version of the agent loaded for all tests unless you change it manually (which might get it out of sync with the version pinned in your build tool). Also, this way you will be loading this agent for all JUnit tests, regardless of if they are using Mockito or not.
I know this is not the best approach, but on the good side, this is just a problem in the IDE. I really hope the folks from JetBrains come up with a solution for this.
Upvotes: 16
Reputation: 339
I want to show Eclipse users how I removed this warning:
Go to RUN submenu/Run Configurations...
Then select the tab (x)-Arguments and type the -XX:+EnableDynamicAgentLoading in the VM arguments field
Upvotes: 3
Reputation: 759
I encountered the same warning.
In our case (I assume you, like me, are using IntelliJ "run configurations" to execute tests), it can be solved by adding the abovementioned parameter "-XX:+EnableDynamicAgentLoading
" to the surefire plugin in your pom.xml
file:
<!-- maven surefire -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>${maven-surefire-plugin.version}</version>
<configuration>
<argLine>-XX:+EnableDynamicAgentLoading</argLine>
</configuration>
</plugin>
Now the warning is gone when running tests from within IntelliJ.
Upvotes: 27
Reputation: 413
Another option if you don't want to customise surefire, gradle or any build script is to modify your .zshrc / .bashrc file (Linux/Mac):
export JDK_JAVA_OPTIONS="-XX:+EnableDynamicAgentLoading"
Or in Windows you can do the usual:
System Properties > Advanced > Environment Variables > Edit
Variable: JDK_JAVA_OPTIONS
Value: -XX:+EnableDynamicAgentLoading
This will affect all your projects & JVMs, so be mindful.
PS: Requires restarting IntelliJ.
Upvotes: 2
Reputation: 7715
For people seeing this using Gradle try the javaagent-test
plugin. Add these to your build.gradle.kts
file. (If you are using something that manages versions then you may not need to add a version to the testJavaagent
plugin.) In theory this is more future proof than using -XX:+EnableDynamicAgentLoading
in case they actually do end up completely disabling dynamic agent loading in the future instead of merely requiring an opt-in.
plugins {
id("com.ryandens.javaagent-test") version "0.5.1"
}
dependencies {
testJavaagent("net.bytebuddy:byte-buddy-agent:1.14.15")
}
Upvotes: 4