为了账号安全,请及时绑定邮箱和手机立即绑定

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

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

阿晨1998 2023-03-23 16:26:38
我目前正在编写一个将代理附加到 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。
查看完整描述

3 回答

?
白衣染霜花

TA贡献1796条经验 获得超10个赞

看来问题出在attach.dll没有从%JAVA_HOME%\jre\bin.

将罐子运行为:

java -Djava.library.path="%JAVA_HOME%\jre\bin" -jar AgentAttacher.jar

似乎有效,只要tools.jar在我的 jar 清单中指定即可Class-Path


查看完整回答
反对 回复 2023-03-23
?
摇曳的蔷薇

TA贡献1793条经验 获得超6个赞

我遇到了同样的问题并以这种方式解决了它:


使用 jdk 11 构建源,找不到tools.jar 或 tools.jar 是 windows


这是我的 pom.xml


    <dependency>

      <groupId>com.sun</groupId>

      <artifactId>tools</artifactId>

      <version>1.8.0</version>

      <scope>system</scope>

      <systemPath>${project.basedir}/lib/tools.jar</systemPath>

    </dependency>

# Create a local directory and put tools. jar in it

mkdir lib && cp %JAVA_8_HOME/lib/tools.jar% lib/

然后:


mvn package

java -jar attach-agent.jar


查看完整回答
反对 回复 2023-03-23
?
呼啦一阵风

TA贡献1802条经验 获得超6个赞

在 Java 8 中,附加 API 是 JVM 默认不加载的单独 jar 的一部分。您必须将其显式包含在类路径中。通常,它位于\libJDK 主目录的文件夹中:

java -cp %JAVA_HOME%\\lib\\tools.jar -jar AgentAttacher.jar


查看完整回答
反对 回复 2023-03-23
  • 3 回答
  • 0 关注
  • 798 浏览

添加回答

举报

0/150
提交
取消
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号