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

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

1.概述

转载:"程序包com.sun.tools.attach不存在"最简单粗暴的解决方案

最近在测试JavaAgent的Attach相关API,代码中有如下实现

  1. import com.sun.tools.attach.VirtualMachine;
  2. public String attachAgentTest(String pid) throws Exception {
  3. if (StringUtils.isEmpty(pid)) {
  4. return "pid can not be empty";
  5. }
  6. VirtualMachine vm = VirtualMachine.attach(pid);
  7. vm.loadAgent("/Users/chenyin/IdeaProjects/JavaLearningDemo/out/artifacts/attach_agent_jar/attach-agent.jar");
  8. return "success";
  9. }

打包编译的时候不断报错:程序包com.sun.tools.attach不存在

解决方案:pom中加入tool.jar外部依赖,指定本地系统中tool.jar路径

  1. <dependency>
  2. <groupId>com.sun</groupId>
  3. <artifactId>tools</artifactId>
  4. <version>1.8.0</version>
  5. <scope>system</scope>
  6. <systemPath>/Library/Java/JavaVirtualMachines/jdk1.8.0_181.jdk/Contents/Home/lib/tools.jar</systemPath>
  7. </dependency>

systemPath请自行更换为自己的Java目录

相关文章

最新文章

更多