在JDK 9及以后的版本中,com.sun.*
包默认是不可见的,因为它们是JDK的内部API,不建议直接使用。这就是为什么在使用JDK 17时,即使com.sun.tools.attach
包存在,你的代码仍然无法访问它。
要解决这个问题,需要在编译你的项目时添加一个JVM参数,来打开com.sun.tools.attach
模块。如果你正在使用Maven,你可以在你的pom.xml
文件中添加以下配置:
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.0</version>
<configuration>
<source>17</source>
<target>17</target>
<compilerArgs>
<arg>--add-exports</arg>
<arg>jdk.attach/com.sun.tools.attach=ALL-UNNAMED</arg>
</compilerArgs>
</configuration>
</plugin>
</plugins>
</build>
这将在编译你的项目时添加--add-exports jdk.attach/com.sun.tools.attach=ALL-UNNAMED
参数,使得com.sun.tools.attach
模块对所有未命名的模块可见。
如果你正在使用IntelliJ IDEA,你可以在Run/Debug Configurations
-> VM options
中添加--add-exports jdk.attach/com.sun.tools.attach=ALL-UNNAMED
参数。