我只是想尝试一下 javassist,然后开始编辑库的方法体。为了连接到库,我使用位于“%JAVA_HOME%..\lib\”中的 tools.jar 附加了一个代理。
但我不喜欢每台电脑都使用它来安装 jdk 只为 tools.jar 的想法
难道没有另一种方法,比如将 jar 的输入提取到我的最终 jar 中吗?
我用 javassist 这样做了,它似乎工作正常(IntelliJ 这样做了。它有一个很好的功能http://puu.sh/hoiCo/bf19853b12.png)
但是使用 tools.jar 这样做会导致程序抛出一个例外
异常截图http://puu.sh/hoiGd/844567bca2.png
public static void main(String[] args){
if(args.length < 1){
log("No ProcessID set");
return;
}
String pid = args[0];
VirtualMachine vm = null;
try{
vm = VirtualMachine.attach(pid);
String filePath = AgentMain.class.getProtectionDomain().getCodeSource().getLocation().getPath();
filePath = URLDecoder.decode(filePath, "UTF-8");
if(filePath.startsWith("/")){
filePath = filePath.substring(1);
}
log("Loading Agent... [" + filePath + "]");
vm.loadAgent(filePath);
}catch(Exception ex){
log("VM connection error [" + pid + "]");
ex.printStackTrace();
}finally{
try{
if(vm != null) vm.detach();
}catch(Exception ex){}
}
}
这是我用于注入代理的代码。
如果有人可以提供帮助,那就太好了。
我希望你明白 :)