我的jdk版本是17,我想使用
com.sun.tools.javac.file.JavacFileManager
类,我的启动参数是
$ jshell --add-modules jdk.compiler
然后我输入了
import com.sun.tools.javac.file.JavacFileManager;
我收到这样的错误:
| Error:
| package com.sun.tools.javac.file is not visible
| (package com.sun.tools.javac.file is declared in module jdk.compiler, which does not export it to the unnamed module)
| import com.sun.tools.javac.file.JavacFileManager;
| ^----------------------^
我该怎么办。
JEP 403 起,不允许访问 JDK 内部模块。
要在 Java 17 中显式允许访问,可以在启动 JVM 时使用选项 --add-exports
。此选项似乎也适用于
jshell
:
jshell --add-exports jdk.compiler/com.sun.tools.javac.file=ALL-UNNAMED
--add-exports
已记录在这里:
--add-exports module/package=target-module(,target-module)*
Updates module to export package to target-module, regardless of module declaration. The target-module can be all unnamed to export to all unnamed modules.