通过 javac 使用内部 sun 类

发布于 2024-09-30 04:46:59 字数 233 浏览 7 评论 0原文

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

不是寻找:

  1. 解释为什么它被禁止。
  2. 建议使用不同的类
  3. 建议使用反射
  4. 建议使用 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:

  1. an explanation why it is forbidden.
  2. suggestion to use different classes
  3. suggestion to use reflection
  4. suggestion to use ecj/eclipse

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(8

独留℉清风醉 2024-10-07 04:46:59

我自己已经找到了答案。

当 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.

ま昔日黯然 2024-10-07 04:46:59

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

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

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.

<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>
            ...
陪你到最终 2024-10-07 04:46:59

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

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

oО清风挽发oО 2024-10-07 04:46:59

如果您使用 Gradle,则需要使用这些选项

compileJava {
    // enable using internal libraries
    options.fork = true
    options.forkOptions.executable = 'javac'
    options.compilerArgs << '-XDignore.symbol.file' }

If you are using Gradle, you need to use these options

compileJava {
    // enable using internal libraries
    options.fork = true
    options.forkOptions.executable = 'javac'
    options.compilerArgs << '-XDignore.symbol.file' }
软的没边 2024-10-07 04:46:59

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

[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;

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

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.

挽心 2024-10-07 04:46:59

还有一个办法就是改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.

漆黑的白昼 2024-10-07 04:46:59

添加到答案,作者:@kamiel-ahmadpour:通过 IntelliJ 运行 Gradle 的编译任务时,我必须设置 javaHome。我的配置现在看起来像这样用 Kotlin DSL 编写:

tasks.withType<JavaCompile> {
    options.isFork = true
    options.forkOptions.executable = "javac"
    options.forkOptions.javaHome = file(System.getProperty("java.home"))
    options.compilerArgs.add("-XDignore.symbol.file")
}

如果没有 javaHome,构建将会失败,并出现以下错误:

... error: package javax.xml.bind.annotation does not exist
import javax.xml.bind.annotation.XmlAccessType;
                            ^

从终端运行 Gradle 任务不需要显式配置的 javaHome我还没有找到不同行为的真正原因。

环境:

  • Java 8
  • Gradle 7.5.1
  • IntelliJ 2022.2.3

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:

tasks.withType<JavaCompile> {
    options.isFork = true
    options.forkOptions.executable = "javac"
    options.forkOptions.javaHome = file(System.getProperty("java.home"))
    options.compilerArgs.add("-XDignore.symbol.file")
}

Without the javaHome the build would fail with errors like:

... error: package javax.xml.bind.annotation does not exist
import javax.xml.bind.annotation.XmlAccessType;
                            ^

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:

  • Java 8
  • Gradle 7.5.1
  • IntelliJ 2022.2.3
瀞厅☆埖开 2024-10-07 04:46:59

如果是 Ant 构建,请将 arg 添加到 javac 中:

<javac>
<compilerarg value="-XDignore.symbol.file" />
</javac>

In case of Ant building, add arg into javac:

<javac>
<compilerarg value="-XDignore.symbol.file" />
</javac>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文