我正在尝试访问 List
来自 com.sun.tools.javac.util
的类(class).这适用于 Java 8,但切换到 Java 9 时出现以下错误:Package 'com.sun.tools.javac.util' is declared in module "jdk.compiler", which does not export it to the unnamed module"
.
我尝试添加 requires jdk.compiler;
到我的module-info
文件,但这并没有解决问题。
最佳答案
从长远来看,处理这种情况的安全方法是不再使用 JDK 的这些内部 API。
可以利用 jdk.compiler
模块的 API 作为 com.sun.tools.javac
的替代品 包。
Defines the implementation of the system Java compiler and its command line equivalent, javac, as well as javah.
特别是 com.sun.tools.javac.util.List
,几乎所有的非重写的、自定义的方法都可以从基于在界面上 java.util.List
.
Migration guide关于 Removed java.*
API 的专栏指出 -
The Java team is committed to backward compatibility. If an application runs in JDK 8, then it will run on JDK 9 as long as it uses APIs that are supported and intended for external use.
These include:
- JCP standard,
java.*
,javax.*
- JDK-specific APIs, some
com.sun.*
, somejdk.*
Supported APIs can be removed from the JDK, but only with notice. Find out if your code is using deprecated APIs by running the static analysis tool
jdeprscan
.
然后添加到上面突出显示的风险..
编译时间
封装在 JDK 9 中的内部 API 在编译时不可访问,但可以通过 --add-exports
命令行选项在编译时访问。
在你的情况下:
--add-exports jdk.compiler/com.sun.tools.javac.util=ALL-UNNAMED
运行时间
在运行时,如果它们在 JDK 8 中,它们仍然可以访问,但在未来的版本中,它们将变得不可访问,此时 --add-exports
或 --add -opens
选项也可用于使它们在运行时也可访问。
关于java - 从 Java 9 访问 com.sun.tools.javac.util,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46773519/