注入 jar 并替换正在运行的 JVM 中的类
Inject Jar and replace classes in running JVM
我希望能够替换一些类并将其添加到已经在运行的JVM中。我读到我需要使用CreateRemoteThread
,但我不完全明白。我读了这篇关于如何做到这一点(软件 RnD(的文章,但我无法弄清楚它的作用和原因。除此之外,它只引入了新类,但不会更改现有类。我怎样才能用C++做到这一点?
你
甚至不需要CreateRemoteThread
- 有一种官方方法可以连接到远程JVM并使用Attach API替换加载的类。
您需要一个调用 Instrumentation.redefineClasses 的 Java 代理。
public static void agentmain(String args, Instrumentation instr) throws Exception { Class oldClass = Class.forName("org.pkg.MyClass"); Path newFile = Paths.get("/path/to/MyClass.class"); byte[] newData = Files.readAllBytes(newFile); instr.redefineClasses(new ClassDefinition(oldClass, newData)); }
您必须添加具有
Agent-Class
属性的MANIFEST.MF
,并将代理打包到 jar 文件中。
然后使用动态附加将代理 jar 注入正在运行的 VM(进程 ID =
pid
(。import com.sun.tools.attach.VirtualMachine; ... VirtualMachine vm = VirtualMachine.attach(pid); try { vm.loadAgent(agentJarPath, options); } finally { vm.detach(); }
文章中有更多细节。
如果您坚持使用 C/C++ 而不是 Java API,您可以查看我的jattach
实用程序。
相关文章:
- 运行同一解决方案的另一个项目的项目
- CMake-按正确顺序将项目与C运行时对象文件链接
- 如何运行位于boost/libs/python/example/tutorial目录中的hello.cpp和Jamfil
- 代码在main()中运行,但在函数中出现错误
- 我在c++代码中生成了一个运行时#3异常
- 如何在linux终端中同时编译和运行c++代码
- 为什么在运行时没有向我们提供有关分段错误的更多信息?
- 如何在运行中期切换GTK CSS style_context
- 如何在MS Visual Studio 2019中运行QT UI
- 如何通过cpp程序运行shell脚本
- IPC使用多个管道和分支进程来运行Python程序
- 删除指向指针的指针是运行时错误吗
- 如何用参数值调用函数(仅在运行时已知)
- 为什么即使使用-cudart-static进行编译,库用户仍然需要链接到cuda运行时
- 如何在C++(JNI)中设置正在运行的JVM的类路径
- 注入 jar 并替换正在运行的 JVM 中的类
- C 在运行的JVM中调用功能
- JNI:从 JVM 到 C++ 的回调无法运行
- 从C++代码运行JVM并设置类路径
- 连接到已经运行的JVM