我使用attach API在运行时加载JVMTI代理。当我的程序完成时,我想卸载JVMTI代理,而不终止加载该代理的JVM。根据this documentation的说法,从attach API无法做到这一点。是否有其他方法可以强制代理通过Java API或从JVMTI代理中卸载其自身?
发布于 2011-06-05 17:59:24
JVMTI卸载(不终止spec says )是可能的,但是依赖于平台并且超出了规范的范围。
发布于 2011-06-07 00:23:41
您必须以编程方式加载JVMTI代理:
// attach to target VM
VirtualMachine vm = VirtualMachine.attach("2177");
// get system properties in target VM
Properties props = vm.getSystemProperties();
// construct path to management agent
String home = props.getProperty("java.home");
String agent = home + File.separator + "lib" + File.separator
+ "your-agent-example.jar";
// load agent into target VM
vm.loadAgent(agent, "com.sun.management.jmxremote.port=5000");
// detach
vm.detach();请参阅doc here
在此之后,您必须使用与默认不同的classLoad:
您必须将系统属性"java.system.class.loader“设置为目标JVM的自定义类加载器的名称。
请参阅doc here
“Java的内置类装入器总是在装入类之前检查它是否已经装入。因此,不能使用Java的内置类装入器重新装入类。要重新装入类,您必须实现自己的ClassLoader子类。”
在本例中,您必须实现一个具有ClassLoader.getSystemClassLoader()的ClassLoader,该a具有父对象。
“即使使用ClassLoader的自定义子类,也会遇到挑战。每个加载的类都需要链接。这是使用ClassLoader.resolve()方法完成的。此方法是最终的,因此不能在resolve子类中重写。resolve()方法不允许任何给定的ClassLoader实例链接同一个类两次。因此,每次要重新加载一个类时,都必须使用ClassLoader子类的新实例。这并不是不可能的,但在设计类重新加载时需要知道这一点。”
请参阅Dynamic Class Reloading
https://stackoverflow.com/questions/6124855
复制相似问题