亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

無法實例化 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 關注
  • 713 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號