0

I am coding a Java Agent library. However, the JVM cannot find Agent_OnLoad function even if I have explicitly exported it in my C++ code.

JNIEXPORT jint JNICALL
    Agent_OnLoad(JavaVM* vm, char* options, void* reserved)
{
    return JNI_OK;
}

It then says Could not find Agent_OnLoad function in the agent library: ./FuncTest.dll. The start-up command line is java -agentpath:./FuncTest.dll -jar .\helloworld.jar

Using Visual Studio 2022, Corretto 17.0.8

2
  • Did you check your actual DLL to make sure the export is present and decorated correctly? You may need to use a .DEF file to name the export properly. It may be getting exported like _Agent_OnLoad or _Agent_OnLoad@12 instead of just plain Agent_OnLoad Commented Jul 13, 2024 at 4:58
  • Thanks @RemyLebeau, I looked up the DLL's functions using IDA and found out that the name was mangled. Commented Jul 13, 2024 at 5:08

1 Answer 1

1

Name decoration is applied to the exported function name.

By editing it to

extern "C" {
    JNIEXPORT jint JNICALL
        Agent_OnLoad(JavaVM* vm, char* options, void* reserved)
    {
        return JNI_OK;
    }
}

solves the problem.

See How to specify a "clean" name of DLL exports? for a detailed answer.

Sign up to request clarification or add additional context in comments.

2 Comments

That is literally the same code you posted originally. Why did you edit your question to change the code just to post the original code in this answer?
@RemyLebeau The original code is simplified from my project. When simplifying the code I modified it's structure wrongly.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.