Unloading a JVMTI agent at runtime?

纵饮孤独 提交于 2019-11-29 04:02:18

JVMTI spec says unloading (without JVM termination) is possible, but platform-dependent and out of specification's scope.

You have to load JVMTI agent programatically :

// 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();

see doc here

After that you have to use a different classLoad than default :

You have to set the system property "java.system.class.loader" to be the name of your custom class loader for your target JVM.

see doc here

"Java's builtin Class loaders always checks if a class is already loaded before loading it. Reloading the class is therefore not possible using Java's builtin class loaders. To reload a class you will have to implement your own ClassLoader subclass."

In your case you have to implement a ClassLoader which has ClassLoader.getSystemClassLoader() has parent.

"Even with a custom subclass of ClassLoader you have a challenge. Every loaded class needs to be linked. This is done using the ClassLoader.resolve() method. This method is final, and thus cannot be overridden in your ClassLoader subclass. The resolve() method will not allow any given ClassLoader instance to link the same class twice. Therefore, everytime you want to reload a class you must use a new instance of your ClassLoader subclass. This is not impossible, but necessary to know when designing for class reloading."

see Dynamic Class Reloading

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!