无法将JVMTI本机代理(DLL)加载到运行的Java程序 - AgentLoadException

时间:2013-06-21 03:42:29

标签: native agent jvmti

我在这个问题上挣扎了几天但是还没有得到正确答案。

以下是问题说明:   我编写了一个普通的Java程序(Program-A),用Agent_OnLoad,Agent_OnAttach,Agent_OnUnload方法编写了一个基于Windows的本机代理(* .dll,用C / C ++编写),如果工作正常使用Java命令行标志(-agentlib)。然后我编写了另一个Java程序来将本机代理附加到运行Java Program-A(参见下面的VM attach和loadAgentPath代码段),但是我得到了例外:

com.sun.tools.attach.AgentLoadException: Failed to load agent library

我尝试以这种或那种方式更改agentPath(绝对或相对文件路径),这些都不起作用。我应该尝试其他方式来使这项工作。我需要的是将本机代理附加到运行的java程序而不是使用命令行标志。

有谁知道解决方案的根本原因或线索?

BTW,运行附加VM Java代码的命令行为:

java -Djava.library.path=D:\DevTools\Java7\jre7\bin -classpath .;./tools.jar com.xxx.TestAgentVMAttacher 
...
VirtualMachine virtualMachine = com.sun.tools.attach.VirtualMachine.attach(pid); // Note: this code line is executed normally, I am sure the pid is correct
...
agentPath = theFilePath + "/myagent.dll"; // Note: I am sure the dll file path is correct 
virtualMachine.loadAgentPath(agentPath,null); // Note: this code line would cause the exception (AgentLoadException) as I mentioned above, no matter how I set the agentPath, even I set it as null, same exception happened.

环境相关信息:
  - 操作系统:Windows XP   - Java版本:Java(TM)SE运行时环境(版本1.7.0-b147)

1 个答案:

答案 0 :(得分:0)

最终我找到了我的问题的答案,我在Agent.cpp文件中有一个错误的方法名称('Agent_Attach'),正确的应该是'Agent_OnAttach',有了这个修复,我的代理lib(.dll)可以现在加载到正在运行的Java程序。