Andrii Khrupalyk
Andrii Khrupalyk

Reputation: 49

Method Agent_OnLoad is called after shutdown jvm

I wrote simple agent:

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

And simple client:

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

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

And if I run program I will see the output below:

Main

Agent start

Why method main invoke before Agent_OnLoad?

Upvotes: 1

Views: 383

Answers (1)

Saqib Ahmed
Saqib Ahmed

Reputation: 1125

The Agent_OnLoad function is being called before main during start of the VM, however the stdout buffer is printing after reaching the newline. Add a \n at the end of printf to see it working. Following answer explains it in detail:

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

Moreover, you can confirm this by putting a breakpoint on the printf (If you're using a debugger) to see that it'd be called at start but won't print until the buffer reaches newline character.

Upvotes: 1

Related Questions