简体   繁体   English

IntelliJ IDEA - 在 IDE 中直接运行时,Java 检测 premain 被调用两次

[英]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.

相关问题 Java Agent Instrumentation premain 中的 ClassNotFoundException - ClassNotFoundException in Java Agent Instrumentation premain 打包库时,IntelliJ IDEA中的Java代理(工具API)java.lang.NoClassDefFoundError - Java Agent (Instrumentation API) java.lang.NoClassDefFoundError in IntelliJ IDEA When Packaging Libraries 如何在 Intellij IDEA 运行之前启用 Hibernate 字节码检测? - How to enable Hibernate bytecode instrumentation just before Intellij IDEA run? 在 Windows 10 上使用多个显示器时,IntelliJ IDEA IDE 会丢失显示器区域 - IntelliJ IDEA IDE gets lost of monitor's area when using more than one monitor on Windows 10 如何在IntelliJ IDEA IDE中运行JavaFX应用程序 - How to run javafx applications in intellij idea IDE 在什么情况下将调用Java代理premain方法? - At what event Java Agent premain method will be called? Java项目在IDE(IntelliJ Idea)中运行,但在Maven构建后无法运行 - Java project runs in IDE (IntelliJ Idea) but fails to run after Maven build Mint中的图标不起作用,但是直接调用时应用程序可以工作-Intellij IDEA - Icon in Mint does not work but application works when called directly - Intellij IDEA 如何从Java IDE为专业开发人员(IntelliJ IDEA)运行SpringBoot应用程序 - How to run a SpringBoot application from Java IDE for Professional Developers (IntelliJ IDEA) IntelliJ IDEA:在IDE外部运行时Jar无法加载图像 - IntelliJ IDEA: Jar doesn't load images when run outside of IDE
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM