通过 javac 使用内部 sun 类
有没有办法禁用 javac 1.6.0_22 的限制,阻止我使用 JRE 内部类,如 sun.awt.event.*
?
我不是寻找:
- 解释为什么它被禁止。
- 建议使用不同的类
- 建议使用反射
- 建议使用 ecj/eclipse
我只是想知道是否可能,如果可能的话如何。
Is there a way to disable restrictions of javac 1.6.0_22 that prevent me from using JRE internal classes like sun.awt.event.*
?
I'm not looking for:
- an explanation why it is forbidden.
- suggestion to use different classes
- suggestion to use reflection
- suggestion to use ecj/eclipse
I just want to know if it is possible or not, and if it is then how.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
我自己已经找到了答案。
当 javac 编译代码时,默认情况下它不会链接到
rt.jar
。相反,它使用带有类存根的特殊符号文件
lib/ct.sym
。令人惊讶的是,这个文件包含许多但不是全部的内部 sun 类。
就我而言,sun.awt.event.IgnorePaintEvent 是那些比平常更内部的类之一。
我的问题的答案是: javac -XDignore.symbol.file
这就是 javac 用于编译 rt.jar 的内容。
I have found the answer myself.
When javac is compiling code it doesn't link against
rt.jar
by default.Instead it uses special symbol file
lib/ct.sym
with class stubs.Surprisingly this file contains many but not all of internal sun classes.
In my case one of those more-internal-than-usual classes was
sun.awt.event.IgnorePaintEvent
.And the answer to my question is:
javac -XDignore.symbol.file
That's what javac uses for compiling
rt.jar
.如果您使用 Maven,除了 @marcin-wisnicki 的答案之外, 请注意,编译器插件将默默地删除任何 -XD 标志,除非您还指定
true
:例如In addition to the answer by @marcin-wisnicki if you're using Maven, note that the compiler plugin will silently drop any -XD flags, unless you also specify
<fork>true</fork>
: e.g.有更好的解决方案。首先将该选项添加到 javac
-XDenableSunApiLintControl
中,然后在代码中使用@SupressWarnings("sunapi")
。There's a better solution. First add the option to javac
-XDenableSunApiLintControl
and then use@SupressWarnings("sunapi")
in your code.如果您使用 Gradle,则需要使用这些选项
If you are using Gradle, you need to use these options
通常,这只会产生一条警告消息;例如,
也许您已经告诉 Java 编译器将警告视为错误。
Normally, this only produces a Warning message; e.g.
Perhaps you have told the Java compiler to treat warnings as errors.
还有一个办法就是改jdk。
就我而言,项目 java 版本 1.8。我从jdk 11开始使用。因此在我的项目中发现了这个错误。所以我把jdk从11改成了1.8。它对我有用。
Another way is change jdk.
In my case project java version 1.8. I used from jdk 11. Therefore This error has found in my project. So I changed my jdk from 11 to 1.8. It has worked for me.
添加到答案,作者:@kamiel-ahmadpour:通过 IntelliJ 运行 Gradle 的编译任务时,我必须设置
javaHome
。我的配置现在看起来像这样用 Kotlin DSL 编写:如果没有
javaHome
,构建将会失败,并出现以下错误:从终端运行 Gradle 任务不需要显式配置的
javaHome
我还没有找到不同行为的真正原因。环境:
Adding to the answer by @kamiel-ahmadpour: I had to set the
javaHome
when running Gradle's compile tasks via IntelliJ. My config now looks like this written in Kotlin DSL:Without the
javaHome
the build would fail with errors like:Running Gradle tasks from the terminal doesn't need the explicitly configured
javaHome
and I didn't find the actual cause for the different behaviour, yet.Environment:
如果是 Ant 构建,请将 arg 添加到 javac 中:
In case of Ant building, add arg into javac: