无法实例化 sun.tools.attach.WindowsAttachProvider

我目前正在编写一个将代理附加到 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。


阿晨1998
浏览 799回答 3
3回答

白衣染霜花

看来问题出在attach.dll没有从%JAVA_HOME%\jre\bin.将罐子运行为:java&nbsp;-Djava.library.path="%JAVA_HOME%\jre\bin"&nbsp;-jar&nbsp;AgentAttacher.jar似乎有效,只要tools.jar在我的 jar 清单中指定即可Class-Path。

摇曳的蔷薇

我遇到了同样的问题并以这种方式解决了它:使用 jdk 11 构建源,找不到tools.jar 或 tools.jar 是 windows这是我的 pom.xml&nbsp; &nbsp; <dependency>&nbsp; &nbsp; &nbsp; <groupId>com.sun</groupId>&nbsp; &nbsp; &nbsp; <artifactId>tools</artifactId>&nbsp; &nbsp; &nbsp; <version>1.8.0</version>&nbsp; &nbsp; &nbsp; <scope>system</scope>&nbsp; &nbsp; &nbsp; <systemPath>${project.basedir}/lib/tools.jar</systemPath>&nbsp; &nbsp; </dependency># Create a local directory and put tools. jar in itmkdir lib && cp %JAVA_8_HOME/lib/tools.jar% lib/然后:mvn packagejava -jar attach-agent.jar

呼啦一阵风

在 Java 8 中,附加 API 是 JVM 默认不加载的单独 jar 的一部分。您必须将其显式包含在类路径中。通常,它位于\libJDK 主目录的文件夹中:java&nbsp;-cp&nbsp;%JAVA_HOME%\\lib\\tools.jar&nbsp;-jar&nbsp;AgentAttacher.jar
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java