比如我首先用java
命令启动了一个程序,然后我想在另外一个java
程序里,获取之前那个程序中的对象并调用相应的方法。
目前能够做到的是,可以使用jdk
提供的工具包tools.jar
里的方法,调用自己写的java agent
:
String pid = "xxxx";
HotSpotVirtualMachine vm = (HotSpotVirtualMachine) new LinuxAttachProvider().attachVirtualMachine(pid);
vm.loadAgent(".../testAgent.jar");
vm.detach();
这里的java agent
可以拿到类的信息
public class TestAgent {
public static void agentmain(String args, Instrumentation inst) throws IllegalAccessException, InstantiationException {
System.out.println("loaded classes: " + inst.getAllLoadedClasses().length);
System.out.println("================");
Class[] classes = inst.getInitiatedClasses(TestAgent.class.getClassLoader());
System.out.println("initiated classes: " + classes.length);
for (Class clazz: classes) {
System.out.println(clazz.getName());
}
System.out.println("================");
}
}
但是似乎拿不到当前正在使用的对象或者引用。
请问有办法可以做到吗?
你需要了解下 进程间通信 —— 最为一般的方式是使用 Socket(RPC)