我目前正在编写一个将代理附加到 JVM 进程的附加程序,但我一直遇到这个问题。这是我的代码的简化版本:
import com.sun.tools.attach.VirtualMachine;
public class AgentAttacher {
public static void main(String[] args) {
try {
String pid = "some-pid-determined-elsewhere";
final VirtualMachine vm = VirtualMachine.attach(pid);
vm.loadAgent("agent.jar");
vm.detach();
} catch (Exception e) {
e.printStackTrace();
}
}
}
运行时java -jar AgentAttacher.jar,出现以下错误:
java.util.ServiceConfigurationError: com.sun.tools.attach.spi.AttachProvider: Provider sun.tools.attach.WindowsAttachProvider could not be instantiated
我试过tools.jar从lib我的 JDK 目录添加到CLASSPATH环境变量,包括在Class-Pathmy 中,并在运行 JAR 时MANIFEST.MF直接指定它。-cp我相当确定它tools.jar正在加载,因为它在丢失时会给出不同的错误:
Exception in thread "main" java.lang.NoClassDefFoundError: com/sun/tools/attach/VirtualMachine
我WindowsAttachProvider could not be instantiated在仅使用时也遇到了错误,所以我认为这与使用不正确的 PIDVirtualMachine.list()无关。attach()
我尝试使用以下方式加载课程Class.forName():
public class AgentAttacher {
public static void main(String[] args) {
try {
Class.forName("sun.tools.attach.WindowsAttachProvider");
} catch (Exception e) {
e.printStackTrace();
}
}
}
我得到以下堆栈跟踪:
Exception in thread "main" java.lang.UnsatisfiedLinkError: no attach in java.library.path
at java.lang.ClassLoader.loadLibrary(Unknown Source)
at java.lang.Runtime.loadLibrary0(Unknown Source)
at java.lang.System.loadLibrary(Unknown Source)
at sun.tools.attach.WindowsAttachProvider.<clinit>(WindowsAttachProvider.java:175)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Unknown Source)
at JavaAttacker.main(JavaAttacker.java:4)
我的环境是带有 JDK 和 JRE 1.8.0_212 的 VirtualBox 上的 Windows 10 Pro (1809) VM。
白衣染霜花
摇曳的蔷薇
呼啦一阵风
相关分类