Getting a java agent has been loaded warning in intelliJ after upgrading JDK 17 to 21
Asked Answered
C

6

20

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

Image of the error, text above

Cirrostratus answered 6/2, 2024 at 23:47 Comment(5)
See JEP 451: Prepare to Disallow the Dynamic Loading of Agents. If you're using Mockito in your tests, then also take a look at JDK 21 - Dynamic Loading of Agent (byte-buddy-agent-1.14.4.jar) #3037. The likely easiest solution is to pass -XX:+EnableDynamicAgentLoading when executing your tests, though all that will do is hide the warning, and might eventually stop working in a future release.Erkan
Based on that JEP and the discussion in the GItHub issue, the proper fix is to load Byte Buddy as an agent (i.e., via -javaagent) instead of as a library (i.e., via --class-path or --module-path). However, it appears that neither IDEs nor build tools make this particularly easy, so properly fixing the problem will likely have to wait until IDEs and build tools "catch up". That said, I believe the GItHub issue has a comment somewhere that shows a solution/workaround for Maven (which you appear to be using).Erkan
Do not use images for logs.Automata
How can I pass -XX:+EnableDynamicAgentLoading ? Where should I exactly write this? @ErkanCirrostratus
Depends on what is executing your tests (Maven, IntelliJ, etc.). Look up how to pass JVM arguments to your tests in whatever tool you're using.Erkan
R
20

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.

Revere answered 25/2, 2024 at 14:22 Comment(0)
A
6

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 {
// previous JVM options
jvmArgs("-noverify", "-XX:+EnableDynamicAgentLoading", "-Djdk.instrument.traceUsage") // add this
}

After making this change, rebuild your project.

Authenticate answered 19/3, 2024 at 18:26 Comment(1)
You only need -XX:+EnableDynamicAgentLoadingAlike
A
1

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")
}
Alike answered 25/6, 2024 at 19:56 Comment(0)
L
0

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.

Lodovico answered 23/7, 2024 at 9:19 Comment(0)
S
0

I want to show Eclipse users how I removed this warning:

Go to RUN submenu/Run Configurations...
Shows the RUN submenu
Then select the tab (x)-Arguments and type the -XX:+EnableDynamicAgentLoading in the VM arguments field
Shows the field where to type -XX:+EnableDynamicAgentLoading

Screen answered 6/8, 2024 at 14:33 Comment(0)
J
-1

In my case this was solved adding the sufire plug-in with the following argLine

 <!-- maven surefire -->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-surefire-plugin</artifactId>
                <version>${maven-surefire-plugin.version}</version>
                <configuration>
                    <argLine>-XX:+EnableDynamicAgentLoading -Xshare:off</argLine>
                </configuration>
            </plugin>
Jezebel answered 7/4, 2024 at 22:35 Comment(1)
This is a duplicate of the answer above, please consider removing this answer.Rhetorical

© 2022 - 2025 — McMap. All rights reserved.