gpt4 book ai didi

java - 关闭jvm后调用方法Agent_OnLoad

转载 作者:行者123 更新时间:2023-11-30 07:04:45 24 4
gpt4 key购买 nike

我写了简单的代理:

JNIEXPORT jint JNICALL Agent_OnLoad(JavaVM *vm, char *options, void *reserved) {    
printf("Agent start");
}

和简单的客户端:

public class Agent {
//java -agentpath:/path_to_lib Agent

public static void main(String[] args) throws InterruptedException {
System.out.println("Main");
}}

如果我运行程序,我将看到以下输出:

Main

Agent start

为什么 main 方法在 Agent_OnLoad 之前调用?

最佳答案

在虚拟机启动期间,Agent_OnLoad 函数在 main 之前被调用,但是 stdout 缓冲区在到达换行符后打印。在 printf 末尾添加 \n 以查看其工作情况。以下答案详细解释了这一点:

Why does printf not flush after the call unless a newline is in the format string?

此外,您可以通过在 printf 上放置断点(如果您使用调试器)来确认这一点,以查看它会在启动时被调用,但直到缓冲区才会打印到达换行字符。

关于java - 关闭jvm后调用方法Agent_OnLoad,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40330957/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com