54

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.

3
  • What errors are you getting exactly? I can compile against sun.awt.* just fine with javac 1.6.0_22 (on the Mac, though).
    – Thilo
    Nov 1, 2010 at 3:00
  • I'm doing some experiments with Swing/AWT and have to access their internals. Nov 1, 2010 at 16:18
  • The same issue applies to other classes like SOAP implementations in com.sun.xml.internal.messaging.saaj.util.* and the same ignore.symbol.file trick (top answer below) is a work-around. Adding this comment here to help folks searching for this problem in that context.
    – jlevy
    Jun 15, 2015 at 22:41

8 Answers 8

97

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.

12
  • I had exactly the same problem, was trying to compile but I was getting this error package sun.nio.fs does not exist for sun.nio.fs.BasicFileAttributesHolder Jan 10, 2013 at 20:19
  • Also see @karmakaze answer for additional workaround for Maven compiler plugin.
    – karmakaze
    May 27, 2015 at 3:16
  • How to add this through command prompt ? i need to apply it for my web app deployed in tomcat under linux
    – Santhosh
    Jul 10, 2015 at 11:42
  • @SanKrish - What part of javac -XDignore.symbol.file do you not understand?
    – Stephen C
    Jul 10, 2015 at 12:35
  • @Stephen Thanks for your reply. i just wanted to config the javac -XDignore.symbol.file for tomcat instead for a class
    – Santhosh
    Jul 10, 2015 at 12:36
42

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>
            ...
4
  • How to add this through command prompt ? i need to apply it for my web app deployed in tomcat under linux
    – Santhosh
    Jul 10, 2015 at 11:43
  • Man, you saved my day with this! Thanks :)
    – Anakin001
    Mar 10, 2016 at 7:36
  • @Anakin001 glad to hear it. These obscure hits are the ones I really like getting used.
    – karmakaze
    Mar 11, 2016 at 18:16
  • Can you please tell me which plugin I can add for the ivy.xml file. Mar 6, 2020 at 7:15
2

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

2

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' }
0

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.

1
  • How to add this through command prompt ? i need to apply it for my web app deployed in tomcat under linux
    – Santhosh
    Jul 10, 2015 at 11:43
0

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.

0

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
0

In case of Ant building, add arg into javac:

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.