从Java 9访问com.sun.tools.javac.util [英] Accessing com.sun.tools.javac.util from Java 9

查看:261
本文介绍了从Java 9访问com.sun.tools.javac.util的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从com.sun.tools.javac.util访问List类.在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".

I'm trying to access the List class from com.sun.tools.javac.util. This works fine with Java 8, but when switching to Java 9 I get the following error: 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文件中,但这不能解决问题.

I tried adding requires jdk.compiler; to my module-info file, but this did not solve the issue.

推荐答案

从长远来看,处理这种情况的安全方法是远离使用JDK的这些内部API.

In the longer run, the safe way to be dealing with such situation is to move away from using these internal APIs of the JDK.

可以利用 jdk.compiler 模块的API,以替代

One can make use of the jdk.compiler module's APIs as a replacement to the com.sun.tools.javac package.

定义系统Java编译器的实现及其命令 等效行 javac 以及 javah .

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.* API 的专栏指出-

Migration guide's column about Removed java.* APIs state that -

Java团队致力于向后兼容.如果 应用程序在JDK 8中运行,那么只要它在JDK 9上运行 使用受支持并打算供外部使用的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.

其中包括:

  • JCP标准, java.* javax.*
  • JDK特定的API,一些 com.sun.* ,一些 jdk.*
  • JCP standard, java.*, javax.*
  • JDK-specific APIs, some com.sun.*, some jdk.*

受支持的API,但只有在得到通知的情况下. 静态分析工具 jdeprscan .

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.

然后添加到上面突出显示的风险中..

Then to add to the highlighted risk above..

JDK 9中封装的内部API在编译时无法访问,但可以通过--add-exports命令行选项在编译时访问.

Internal APIs that are encapsulated in JDK 9 are not accessible at compile time, but can be made accessible at compile time via the --add-exports command-line option.

在您的情况下:

--add-exports jdk.compiler/com.sun.tools.javac.util=ALL-UNNAMED

运行时间

在运行时,如果它们在JDK 8中,则它们仍可访问,但在将来的版本中,它们将变得不可访问,这时--add-exports--add-opens选项也可用于使它们在运行时也可访问

Run Time

At the runtime, they remain accessible if they were in JDK 8 but in a future release, they will become inaccessible, at which point the --add-exports or --add-opens options can be used to make them accessible at runtime as well.

这篇关于从Java 9访问com.sun.tools.javac.util的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆