天天看點

【java】“程式包com.sun.tools.attach不存在“最簡單粗暴的解決方案1.概述

【java】“程式包com.sun.tools.attach不存在“最簡單粗暴的解決方案1.概述

1.概述

轉載:"程式包com.sun.tools.attach不存在"最簡單粗暴的解決方案

最近在測試JavaAgent的Attach相關API,代碼中有如下實作

import com.sun.tools.attach.VirtualMachine;

public String attachAgentTest(String pid) throws Exception {
    if (StringUtils.isEmpty(pid)) {
        return "pid can not be empty";
    }
    VirtualMachine vm = VirtualMachine.attach(pid);
    vm.loadAgent("/Users/chenyin/IdeaProjects/JavaLearningDemo/out/artifacts/attach_agent_jar/attach-agent.jar");
    return "success";
}
           

打包編譯的時候不斷報錯:程式包com.sun.tools.attach不存在

【java】“程式包com.sun.tools.attach不存在“最簡單粗暴的解決方案1.概述

解決方案:pom中加入tool.jar外部依賴,指定本地系統中tool.jar路徑

<dependency>
    <groupId>com.sun</groupId>
    <artifactId>tools</artifactId>
    <version>1.8.0</version>
    <scope>system</scope>
    <systemPath>/Library/Java/JavaVirtualMachines/jdk1.8.0_181.jdk/Contents/Home/lib/tools.jar</systemPath>
</dependency>
           

systemPath請自行更換為自己的Java目錄