Java 11 Maven项目引入com.sun.tools.javac私有类的求助
解决JDK 11中com.sun.tools.javac私有类的编译与IDE可见性问题
问题描述
我需要使用com.sun.tools.javac下的私有类,但这些类在编译和运行时均不可见。我的开发环境如下:
- JDK 11.0.15
- Maven构建工具
- IntelliJ IDEA
目前代码中的导入语句标红,执行mvn compile时编译失败。我希望这些类能在IDE编写代码时、编译阶段以及运行时都正常可用。
需要访问的com.sun.tools.javac子包包括:
- com.sun.tools.javac.code
- com.sun.tools.javac.comp
- com.sun.tools.javac.file
- com.sun.tools.javac.main
- com.sun.tools.javac.model
- com.sun.tools.javac.parser
- com.sun.tools.javac.processing
- com.sun.tools.javac.tree
- com.sun.tools.javac.util
当前的pom.xml配置片段如下:
... <properties> <maven.compiler.source>11</maven.compiler.source> <maven.compiler.target>11</maven.compiler.target> </properties> <build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version>3.8.0</version> <configuration> <source>11</source> <target>11</target> </configuration> </plugin> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-javadoc-plugin</artifactId> <version>2.9.1</version> <executions> <execution> <id>attach-javadocs</id> <goals> <goal>jar</goal> </goals> </execution> </executions> </plugin> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-source-plugin</artifactId> <version>2.2.1</version> <executions> <execution> <id>attach-sources</id> <goals> <goal>jar</goal> </goals> </execution> </executions> </plugin> </plugins> </build>
解决方案
从JDK 9开始,模块化系统限制了对内部API(比如com.sun.tools.javac下的类)的访问,需要通过添加JVM参数来开放这些包的访问权限。
1. 修改Maven编译配置
更新maven-compiler-plugin的配置,添加compilerArgs参数,将需要的包导出并开放给未模块化项目(用ALL-UNNAMED,如果是模块化项目则替换为你的模块名):
<plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version>3.8.0</version> <configuration> <source>11</source> <target>11</target> <compilerArgs> <!-- 导出包供编译时访问 --> <arg>--add-exports=jdk.compiler/com.sun.tools.javac.code=ALL-UNNAMED</arg> <arg>--add-exports=jdk.compiler/com.sun.tools.javac.comp=ALL-UNNAMED</arg> <arg>--add-exports=jdk.compiler/com.sun.tools.javac.file=ALL-UNNAMED</arg> <arg>--add-exports=jdk.compiler/com.sun.tools.javac.main=ALL-UNNAMED</arg> <arg>--add-exports=jdk.compiler/com.sun.tools.javac.model=ALL-UNNAMED</arg> <arg>--add-exports=jdk.compiler/com.sun.tools.javac.parser=ALL-UNNAMED</arg> <arg>--add-exports=jdk.compiler/com.sun.tools.javac.processing=ALL-UNNAMED</arg> <arg>--add-exports=jdk.compiler/com.sun.tools.javac.tree=ALL-UNNAMED</arg> <arg>--add-exports=jdk.compiler/com.sun.tools.javac.util=ALL-UNNAMED</arg> <!-- 开放包供运行时反射访问(按需添加) --> <arg>--add-opens=jdk.compiler/com.sun.tools.javac.code=ALL-UNNAMED</arg> <arg>--add-opens=jdk.compiler/com.sun.tools.javac.comp=ALL-UNNAMED</arg> <arg>--add-opens=jdk.compiler/com.sun.tools.javac.file=ALL-UNNAMED</arg> <arg>--add-opens=jdk.compiler/com.sun.tools.javac.main=ALL-UNNAMED</arg> <arg>--add-opens=jdk.compiler/com.sun.tools.javac.model=ALL-UNNAMED</arg> <arg>--add-opens=jdk.compiler/com.sun.tools.javac.parser=ALL-UNNAMED</arg> <arg>--add-opens=jdk.compiler/com.sun.tools.javac.processing=ALL-UNNAMED</arg> <arg>--add-opens=jdk.compiler/com.sun.tools.javac.tree=ALL-UNNAMED</arg> <arg>--add-opens=jdk.compiler/com.sun.tools.javac.util=ALL-UNNAMED</arg> </compilerArgs> </configuration> </plugin>
2. 配置IntelliJ IDEA
为消除IDE标红并启用代码提示,需添加对应VM参数:
- 打开
File > Project Structure > Modules,选中你的模块,确保JDK 11配置正常 - 进入
Run > Edit Configurations,在运行配置的VM options中添加所有--add-exports和--add-opens参数 - 打开
File > Settings > Build, Execution, Deployment > Compiler > Java Compiler,在Additional command line parameters中添加所有--add-exports参数
3. 运行时配置
如果通过Maven运行项目(如mvn exec:java),需在exec-maven-plugin中添加VM参数:
<plugin> <groupId>org.codehaus.mojo</groupId> <artifactId>exec-maven-plugin</artifactId> <version>3.0.0</version> <configuration> <mainClass>你的主类全路径</mainClass> <jvmArguments> --add-exports=jdk.compiler/com.sun.tools.javac.code=ALL-UNNAMED --add-exports=jdk.compiler/com.sun.tools.javac.comp=ALL-UNNAMED <!-- 其他需要的--add-exports和--add-opens参数 --> </jvmArguments> </configuration> </plugin>
注意事项
- 若项目是模块化的(存在
module-info.java),需将ALL-UNNAMED替换为你的模块名称 - 内部API不属于JDK公开规范,可能在后续版本中变更或移除,需注意兼容性风险
内容的提问来源于stack exchange,提问作者Serhii Kachan
相关产品推荐
相关产品推荐

