[英]IntelliJ IDEA - Java instrumentation premain gets called twice when directly run inside IDE
For some reason the premain
of my Java Agent is executed twice when I run a program inside of IDEA and add the Agent jar via the IDEA VM options:出于某种原因,当我在 IDEA 中运行程序并通过 IDEA VM 选项添加代理 jar 时,我的 Java 代理的premain
执行了两次:
I have the following sample program and added the Java Agent in IDEA via我有以下示例程序并通过以下方式在 IDEA 中添加了 Java Agent
Run Configuration
-> VM options
: -javaagent:/path/to/agent/MyJavaAgent.jar
Run Configuration
-> VM options
: -javaagent:/path/to/agent/MyJavaAgent.jar
package com.example;
public class Test {
public static void main(String[] args) {
System.out.println("Hello World!");
}
}
The MyJavaAgent.jar
has the following structure: MyJavaAgent.jar
具有以下结构:
.
├── com
│ └── example
│ └── MyJavaAgent.class
└── META-INF
└── MANIFEST.MF
MyJavaAgent.java MyJavaAgent.java
package com.example;
import java.lang.instrument.Instrumentation;
public class MyJavaAgent {
private static int callCount = 0;
public static void premain(String agentArgs, Instrumentation inst) {
callCount++;
System.out.println("premain call " + callCount);
}
}
MANIFEST.MF清单文件
Manifest-Version: 1.0
Premain-Class: com.example.MyJavaAgent
Can-Redefine-Classes: true
Can-Retransform-Classes: true
Can-Set-Native-Method-Prefix: true
When I run the sample Test.main()
I get the following output:当我运行示例Test.main()
我得到以下输出:
> Task :Test.main()
premain call 1
premain call 2
Hello World!
When I run the program without IDEA everything looks like expected:当我在没有 IDEA 的情况下运行程序时,一切看起来都符合预期:
> java -javaagent:/path/to/agent/MyJavaAgent.jar com.example.Test
premain call 1
Hello World!
Can someone explain what is happening here?有人可以解释这里发生了什么吗? Is that an IDEA bug?这是IDEA的错误吗?
I'm using IntelliJ IDEA 2019.3.4 (Ultimate Edition), Build #UI-193.6911.18
我使用的是IntelliJ IDEA 2019.3.4 (Ultimate Edition), Build #UI-193.6911.18
It appears to be related to how IntelliJ IDEA is starting your class via Gradle by generating the task on the fly.这似乎与 IntelliJ IDEA 如何通过 Gradle 动态生成任务来启动您的课程有关。
The workaround is to disable the run delegation to Gradle .解决方法是禁用对 Gradle 的运行委托。
I've reported a bug that you can follow for updates.我报告了一个错误,您可以关注该错误以获取更新。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.