在 javac 中使用内部 sun 类

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4065401/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-14 11:10:11  来源:igfitidea点击:

Using internal sun classes with javac

javajavacinternal

提问by Marcin Wisnicki

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.*?

有没有办法禁用阻止我使用 JRE 内部类的 javac 1.6.0_22 的限制sun.awt.event.*

I'm notlooking for:

不是在寻找:

  1. an explanation why it is forbidden.
  2. suggestion to use different classes
  3. suggestion to use reflection
  4. suggestion to use ecj/eclipse
  1. 解释为什么它被禁止。
  2. 建议使用不同的类
  3. 建议使用反射
  4. 建议使用 ecj/eclipse

I just want to know if it is possible or not, and if it is then how.

我只想知道这是否可能,如果可能,那么如何。

采纳答案by Marcin Wisnicki

I have found the answer myself.

我自己找到了答案。

When javac is compiling code it doesn't link against rt.jarby default. Instead it uses special symbol file lib/ct.symwith class stubs.

当 javac 编译代码时,它rt.jar默认不链接。相反,它使用lib/ct.sym带有类存根的特殊符号文件。

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.

令人惊讶的是,该文件包含许多但不是全部的内部 sun 类。在我的情况下,那些比平常更内部的类之一是sun.awt.event.IgnorePaintEvent.

And the answer to my question is: javac -XDignore.symbol.file

我的问题的答案是: javac -XDignore.symbol.file

That's what javac uses for compiling rt.jar.

这就是 javac 用于编译rt.jar.

回答by Stephen C

Normally, this only produces a Warning message; e.g.

通常,这只会产生警告消息;例如

[javac] /media/disk/opensso2/opensso/products/federation/openfm/source/com/sun/identity/wss/xmlsig/WSSSignatureProvider.java:46: warning: com.sun.org.apache.xpath.internal.XPathAPI is Sun proprietary API and may be removed in a future release
[javac] import com.sun.org.apache.xpath.internal.XPathAPI;

Perhaps you have told the Java compiler to treat warnings as errors.

也许您已经告诉 Java 编译器将警告视为错误。

回答by karmakaze

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.

如果您使用 Maven,除了@marcin-wisnicki 的回答之外,请注意编译器插件将默默地删除任何 -XD 标志,除非您还指定 <fork>true</fork>:例如

<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>3.3</version>
            <configuration>
                <source>1.7</source>
                <target>1.7</target>
                <compilerArgs>
                    <arg>-XDignore.symbol.file</arg>
                </compilerArgs>
                <fork>true</fork>
            </configuration>
            ...

回答by juancn

There's a better solution. First add the option to javac -XDenableSunApiLintControland then use @SupressWarnings("sunapi")in your code.

有一个更好的解决方案。首先将选项添加到 javac -XDenableSunApiLintControl,然后@SupressWarnings("sunapi")在您的代码中使用。