【java】“程序包com.sun.tools.attach不存在“最简单粗暴的解决方案

x33g5p2x  于2022-06-20 转载在 Java  
字(0.8k)|赞(0)|评价(0)|浏览(267)

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不存在

解决方案: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目录

相关文章

微信公众号

最新文章

更多